Markdown Bash Code Block: Highlight Shell Code in README.md

You wrote a README.md or a wiki page, pasted some terminal commands into it, and they show up as flat grey text. Or the colours are there on GitHub but missing in another viewer. The fix is the word you put right after the opening backticks.

To get a markdown bash code block, open the fence with three backticks and the tag bash, paste the commands, and close with three backticks. Use sh, shell or zsh for the same result, and console when you want to show a prompt followed by output. The tags below were checked against GitHub Linguist and the GitHub code block docs. Rendering of the samples was checked with two Markdown parsers (see the summary for what was not tested).

The animation shows the same three lines with no tag, with bash and with console.

Animation of the same shell lines in a README.md code fence with no tag, the bash tag and the console tag, showing how the rendered block changes
Illustration drawn for this article. Real colours depend on the site and its theme.

The sections below cover these CLI tags, plus where each one is highlighted and the mistakes that switch highlighting off.

  • Shell and Bash
  • Console for prompt plus output
  • Powershell
  • DOS (bat, cmd)

Command Pattern

A fenced code block is three backticks, an optional language name (the info string), your code, and three closing backticks. The opening and closing lines must each sit on their own line. This is the general pattern for highlighting code in the language of your choice:

“`<language> // your code here “`

Tilde fences (~~~) work the same way in CommonMark, as long as the closing fence uses the same character and is at least as long as the opening one.

Shell – Using Console for bash/shell code in Markdown

Use console when the block is a terminal session: a prompt, the command, then what the tool printed. In GitHub Linguist console is an alias of the ShellSession language (together with bash session), which is why the prompt and the output get different treatment.

Example

“`console $ sudo service redis-server restart Stopping redis-server: redis-server. Starting redis-server: redis-server. “`

GitHub preview

GitHub preview of a console code block in README.md showing the sudo service redis-server restart command and its output

That is how the block looks with the syntax highlighter in GitHub.

Bash – Using Bash/shell command in Markdown

For scripts and commands that people will run, use bash (or sh). Both of these open the same Shell grammar on GitHub:

“`sh sudo service redis-server restart “` OR “`bash sudo service redis-server restart “`

In Linguist the Shell language lists the aliases sh, shell-script, bash and zsh. Pick one tag and use it everywhere in the repository so the files stay consistent.

GitHub preview of a bash code block in README.md with the redis-server restart command highlighted

Which sites highlight which tags

There is no single Markdown highlighter. Each site picks its own, so the same tag can look different. This is what the official documentation says, and which rows could not be confirmed:

WhereHighlighter and ruleChecked
GitHub (README, wiki)Linguist decides the grammar. Valid keywords are in the languages YAML file.Yes, GitHub docs and languages.yml
GitLabRouge library. Add the language after the three backticks. Inline code is not highlighted.Yes, GitLab docs. Per-tag support: see the Rouge list
Azure DevOps wikihighlight.js identifiers after three backticks. The Markdown dashboard widget shows plain text.Yes, Microsoft Learn
Stack Overflowhighlight.js. The language is often inferred from the question tags, or set with lang-bash style tags.Yes, editing help page
Bitbucket Data CenterCodeMirror. Add the language name after the backticks.Yes for Data Center docs. Bitbucket Cloud page not reachable, so not verified
VS Code Markdown previewBuilt-in preview of fenced blocks.Not verified which tags are coloured. Open the preview and look

Details worth knowing: GitHub documents that the identifiers are looked up in Linguist, and GitLab says it uses Rouge (GitLab Markdown docs). Azure DevOps points to the highlight.js language list (Azure DevOps Markdown guidance). In highlight.js, bash has the aliases sh and zsh, while the session style language answers to console and shellsession. That was read from the highlight.js source on its main branch, not from the exact version each site runs.

Powershell – Using the Powershell command in Markdown

PowerShell uses its own tag. In Linguist the aliases are posh and pwsh, and the file extension is .ps1. Do not put Linux commands in a powershell block, because the colours follow the PowerShell grammar:

“`powershell Get-Service -Name Spooler | Restart-Service Write-Host "Spooler restarted" “`

DOS – Using bat or cmd code blocks in Markdown

For Windows batch files use bat (Linguist Batchfile aliases: bat, batch, dosbatch, winbatch). highlight.js also accepts dos and cmd, which is what Azure DevOps wikis and Stack Overflow use. Whether GitHub accepts cmd or dos as a tag was not verified, so prefer bat on GitHub.

“`bat @echo off echo Restarting service net stop Spooler && net start Spooler “`

Indented code blocks vs fenced code blocks

Markdown has two ways to make a code block. A fenced block uses backticks and accepts a language tag. An indented block is any run of lines indented by four spaces, and it has no place for a language, so it is never language-highlighted:

sudo service redis-server restart echo "no language tag possible here"

Azure DevOps documents the same behaviour: a line that starts with four spaces converts to a code block, and the docs recommend three backticks so you can name the language. Use fences for shell code.

Inline code and escaping backticks

For a command or a file name inside a sentence, wrap it in single backticks, like npm install. GitLab states inline code is never syntax highlighted, and a language tag cannot be added to it. So put anything longer than a short command in a fenced block.

To show the fence itself, for example in a style guide, wrap it in a longer fence. A closing fence must be at least as long as the opening one, so four backticks can contain three:

““markdown “`bash echo "nested fence" “` ““

GitHub documents the same trick: wrap triple backticks inside quadruple backticks. If the code itself contains a line of three backticks (a shell heredoc that writes Markdown, for instance), use a fence of four or more, or switch to tildes.

Make commands copy friendly

  • Keep prompts out of blocks people will paste. A $ at the start of a line is copied along with the command and then fails in the shell.
  • Split input and output. Put the commands in a bash block, then show the result in a separate console or plaintext block.
  • Use \ line continuation only at the end of a line, with nothing after it, so a pasted multi-line command still works.
  • Quote values that contain spaces or $ so the pasted line behaves the same in bash and zsh.
  • Use a comment line starting with # above a command to explain it, instead of trailing text that would be pasted too.
“`bash # 1. restart the service sudo service redis-server restart # 2. check it answers redis-cli ping “`
“`console $ redis-cli ping PONG “`

Common mistakes

  • Backticks not on their own line: the opening and closing fences must each start a line, with no text before them.
  • Wrong tag spelling: Bash may work on one site and fail on another. Use lower case, as GitHub documents for GitHub Pages sites.
  • Indented block inside a list: an indented code block inside a list item needs more indentation. Prefer a fence indented to the list text.
  • Tag that the site does not know: an unknown word still renders a code box, just without colours. Try sh or bash instead.
  • Closing fence shorter than the opening: a three-backtick close does not end a four-backtick block, so the rest of the page turns into code.
  • Smart quotes pasted from a word processor: curly quotes in the code break the command even if the colours are right.

Related posts: display a table in README.md, add JSON to a Git README and add images to README.md.

Markdown bash code block cheat sheet

You want to showFence tagNotes
A script or commands to runbash (or sh, zsh)Aliases of Shell in Linguist
Prompt plus outputconsoleShellSession in Linguist; no prompt in code people copy
Output onlytext or no tagNot coloured
PowerShellpowershell (or pwsh)Not for Linux commands
Windows batchbatcmd and dos are highlight.js aliases only
Fence inside a fenceFour backticks outsideClosing fence at least as long as the opening
Command inside a sentenceSingle backticksNever highlighted

Frequently asked questions

What is the language tag for bash in a Markdown code block?

Write bash after the opening three backticks. sh, shell and zsh also work on GitHub, where they map to the same Shell grammar. Use console when the block shows a terminal session with a $ prompt and output.

How do I show a terminal prompt and its output in a Markdown code block?

Open the fence with console and start each command line with $ . Output lines follow without the prefix. If readers will copy the block, use a bash block for the commands and a separate console or plaintext block for the output.

How do I put triple backticks inside a Markdown code block?

Wrap the block in a longer fence. Four backticks on the opening and closing lines let you show three backticks inside, because a closing fence must be at least as long as the opening one. A tilde fence also works.

Summary

Open a fence with three backticks and a tag: bash for commands, console for a prompt with output, powershell and bat for the Windows shells. Each site chooses its own highlighter, so check the result where your readers will see it. Tested here: the sample README (bash, sh, shell, zsh, console, untagged, tilde, indented and four-backtick blocks) rendered with markdown-it-py and Python-Markdown, which confirmed the fences parse and carry the language name. Not tested: the actual colours on GitHub, GitLab, Azure DevOps, Bitbucket, Stack Overflow and VS Code, the copy button of each site, and Bitbucket Cloud.

That’s all. References: GitHub: creating and highlighting code blocks, Linguist languages.yml.

Found this useful?

Bookmark this page and share it with your team. Get new .NET, cloud and database tutorials in your inbox.

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Leave a Reply

Your email address will not be published. Required fields are marked *