545

How can I highlight the Bash/shell commands in Markdown files?


For example, to highlight js, I write:

```js
function () { return "This code is highlighted as Javascript!"}
```

To highlight HTML code I use ```html.

How can we highlight Bash/shell commands?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474

8 Answers8

611

If you are looking to highlight a shell session command sequence as it looks to the user (with prompts, not just as contents of a hypothetical script file), then the right identifier to use at the moment is console:

```console
foo@bar:~$ whoami
foo
```

GitHub Markdown preview tab screenshot

Anton Strogonoff
  • 32,294
  • 8
  • 53
  • 61
  • 3
    Nice! Is there a reference for this standard? Is it git flavored md or something else? Thanks! – Jorge Orpinel Pérez Apr 19 '19 at 00:30
  • 6
    @JorgeOrpinel I believe I dug it up in the list of languages supported by GitHub’s syntax highlighter [here](https://github.com/github/linguist/blob/master/lib/linguist/languages.yml#L4947). – Anton Strogonoff Dec 01 '19 at 14:30
  • 8
    https://github.com/github/linguist/blob/3c3b037910006fc2f1a9bb34b2c4e9cde062206c/lib/linguist/languages.yml#L5842-L5843 has listed both `console` and `bash session` as aliases for this `ShellSession` language, so my understanding is one could use `console`, `bash session` or `ShellSession` to achieve this but in reality only `console` and `shellsession` (case doesn't matter) worked for me in testing just now. `bash session` probably does work but I can't quote it right to include the space, so it just picks up `bash` in the beginning and drops `session`. – Elijah Lynn Mar 11 '22 at 17:06
  • 2
    Do you know if the recommended identifier has changed? When I try `console` here at stackoverflow, I don't get any syntax highlighting. And `shell` and `bash` seem to think that everything is shell code, even command output (which are rows that don't include any `$`, I guess?). – HelloGoodbye Aug 05 '22 at 12:51
  • @HelloGoodbye yes, it looks like it might not work as of today. I’m not sure if it’s intentional, since syntax highlighting on SO is [claimed](https://stackoverflow.com/editing-help#syntax-highlighting) to work via highlight.js which apparently [does](https://github.com/highlightjs/highlight.js/blob/6b8c831f00c4e87ecd2189ebbd0bb3bbdde66c02/src/languages/shell.js#L13-L29) support `console` with prompts. I’m not feeling determined enough right now to see what version of highlight.js is used at SO and how it’s integrated… Could be a regression of some sort. – Anton Strogonoff Aug 07 '22 at 07:40
  • That said, just to keep it clear, the question posted originally is about GitHub-flavoured implementation of Markdown, which still relies on `linguist`, which still [supports](https://github.com/github/linguist/blob/249bbd1c2ffc631ca2ec628da26be5800eec3d48/lib/linguist/languages.yml#L6084-L6095) `console`. What’s used at SO seems like a different question. – Anton Strogonoff Aug 07 '22 at 07:45
  • (A question about StackOverflow itself may be best suited for the “meta” Q&A site.) – Anton Strogonoff Aug 07 '22 at 07:49
487

It depends on the Markdown rendering engine and the Markdown flavour. There is no standard for this. If you mean GitHub flavoured Markdown for example, shell should work fine. Aliases are sh, bash or zsh. You can find the list of available syntax lexers here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    I provided a bit more details on how to deduce language specifiers for Markdown from the linked file above here: https://stackoverflow.com/a/45786100/6884590, in case that's useful to anyone finding this question. – pchaigno Jan 08 '18 at 12:07
  • 1
    Thanks. Also, `fish` is a valid language too. I didn't edit the answer because the question was around bash, but think it might be useful to others. – Elijah Lynn Mar 07 '23 at 22:07
82

I found a good description at Markdown Cheatsheet:

Code blocks are part of the Markdown spec, but syntax highlighting isn't.

However, many renderers -- like GitHub's and Markdown Here -- support syntax highlighting. Which languages are supported and how those language names should be written will vary from renderer to renderer. Markdown Here supports highlighting for dozens of languages (and not-really-languages, like diffs and HTTP headers); to see the complete list, and how to write the language names, see the highlight.js demo page.

Although I could not find any official GitHub documentation about using highlight.js, I've tested lots of languages and seemed to be working

To see list of languages I used https://github.com/highlightjs/highlight.js/blob/master/SUPPORTED_LANGUAGES.md

Some shell samples:

Shell:      console, shell
Bash:       bash, sh, zsh
PowerShell: powershell, ps
DOS:        dos, bat, cmd

Example:

```bat
cd \
copy a b
ping 192.168.0.1
```
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
50

If I need only to highlight the first word as a command, I often use properties:

```properties
npm run build
```  

I obtain something like:

npm run build

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
g.annunziata
  • 3,118
  • 1
  • 24
  • 25
17

Using the knitr package:

```{r, engine='bash', code_block_name} ...

E.g.:

```{r, engine='bash', count_lines}
wc -l en_US.twitter.txt
```

You can also use:

  • engine='sh' for shell
  • engine='python' for Python
  • engine='perl', engine='haskell' and a bunch of other C-like languages and even gawk, AWK, etc.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
irJERAD
  • 590
  • 5
  • 6
17

Per the documentation from GitHub regarding GFM syntax highlighted code blocks

We use Linguist to perform language detection and syntax highlighting. You can find out which keywords are valid in the languages YAML file.

Rendered on GitHub, console makes the lines after the console blue. bash, sh, or shell don't seem to "highlight" much ...and you can use posh for PowerShell or CMD.

MmmHmm
  • 3,435
  • 2
  • 27
  • 49
2

Bitbucket uses CodeMirror for syntax highlighting. For Bash or shell you can use sh, bash, or zsh. More information can be found at Configuring syntax highlighting for file extensions and Code mirror language modes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

In Obsidian, you can use sh-session

https://prismjs.com/

wuan
  • 136
  • 1
  • 7