Bash Syntax Highlighting in Markdown: ```bash vs ```sh
Learn reliable syntax highlighting for Bash and shell scripts in Markdown using ```bash, ```sh, or ```console. Examples for GitHub, VS Code, scripts vs sessions, common errors, and editor setups for perfect code coloring.
Use bash. Some editors also support sh.
For syntax highlighting Bash and shell scripts in Markdown, use fenced code blocks with bash—it's the most reliable identifier across platforms like GitHub, VS Code, and Obsidian. Many renderers also accept sh or shell as aliases, thanks to GitHub Linguist's grammar mappings. For interactive sessions with prompts, switch to console to avoid syntax errors and get proper coloring for commands like echo or sudo.
Contents
- What is Syntax Highlighting in Markdown
- Correct Language Identifiers for Bash
- Highlighting Interactive Shell Sessions
- Bash Script Examples in GitHub Markdown
- Editor Setup for Syntax Highlighting
- Common Errors and Fixes
- Platform-Specific Tips and Alternatives
- Sources
- Conclusion
What is Syntax Highlighting in Markdown
Ever pasted a bunch of Bash commands into a README or note, only to see a wall of unreadable gray text? That’s where syntax highlighting saves the day. Markdown fenced code blocks—those triple backticks—let you specify a language like bash, triggering colored output for keywords, strings, and comments. No universal standard exists, but renderers like GitHub’s Linguist or Prism.js handle it by mapping identifiers to grammar files.
Why bother for shell scripts? It makes code scannable. Imagine debugging a bash syntax error near unexpected token—colors spot mismatches instantly. Platforms parse the ID after the backticks (e.g., ```bash) and apply themes, usually orange for commands, green for vars. But it varies: GitHub loves lowercase, while some editors choke on uppercase.
Correct Language Identifiers for Bash
```bash
#!/bin/bash
echo "Hello, world!"
for i in {1..3}; do
echo "Loop: $i"
done
GitHub renders this with proper bash syntax highlighting: shebangs in magenta, variables in blue. Stack Overflow consensus backs it—over 500 upvotes for bash or sh.
Aliases matter. Linguist’s languages.yml maps:
- bash → Shell grammar
- sh → Shell
- shell → Shell
So sh or shell? They pipe to the same highlighter. But stick to bash for clarity—it's explicit. Uppercase like BASH fails on GitHub. Spaces? Nope, ```bash script breaks it.
| Identifier | Works on GitHub? | Best For | Aliases To |
|---|---|---|---|
| ```bash | Yes | Scripts | Shell |
| ```sh | Yes | POSIX shell | Shell |
| ```shell | Yes | Generic | Shell |
| ```zsh | Partial | Zsh-specific | Shell |
Test in your renderer. VS Code? Spot on.
Highlighting Interactive Shell Sessions
Scripts are clean, but what about terminal copy-pastes? Prompts like $ or > confuse script highlighters, turning everything gray. Use ```console instead.
$ sudo apt update
Reading package lists... Done
$ echo $PATH
/usr/bin:/bin:/usr/local/bin
This gets session-style coloring—no syntax errors flagged on prompts. Alternatives: shellsession or sh-session, per community tips.
Why not ```bash here? It treats $ as a var expansion fail. Console mode ignores that, focusing on command flow. GitHub supports it natively via Linguist.
Quick hack for mixed content: Embed sessions in docs with here-docs, but flag as console.
Bash Script Examples in GitHub Markdown
Real-world test. Save this as README.md on GitHub:
## Backup Script
```bash
#!/bin/bash
# Simple backup with args
BACKUP_DIR="$1"
if [ -z "$BACKUP_DIR" ]; then
echo "Usage: $0 <dir>" >&2
exit 1
fi
tar -czf "backup_$(date +%Y%m%d).tar.gz" "$BACKUP_DIR"
```
Push it—bam, highlighted. Handles loops, conditionals, args perfectly. For errors? Colors flag unmatched quotes.
Another: Argument parsing.
#!/bin/bash
while [[ $# -gt 0 ]]; do
case $1 in
-f|--file) FILE="$2"; shift 2 ;;
*) echo "Unknown: $1"; exit 1 ;;
esac
done
GitHub docs confirm 300+ languages, bash included.
Editor Setup for Syntax Highlighting
VS Code? Install “Markdown All in One” extension—it uses Prism for live previews with ```bash support. Theme it dark for that GitHub vibe.
Vim users: Add to .vimrc:
autocmd FileType markdown syntax on
let g:markdown_fenced_languages = ['bash', 'sh', 'console']
Notepad++? Plugins like “Markdown Panel” + language config. Obsidian shines with ```bash out of the box—custom CSS for themes.
Pro tip: Preview mismatch? Check your extension’s grammar list. Prism aliases shell to bash, per its repo.
Common Errors and Fixes
Bash syntax error in Markdown? Usually renderer gripes.
| Issue | Cause | Fix |
|---|---|---|
| Gray code | Wrong ID | Use bash, not script |
| Prompt errors | Script mode on session | Switch to ```console |
| No color | Uppercase/spaces | Lowercase, no spaces: ```sh |
| VS Code blank | Extension missing | Install Prism-based Markdown preview |
Unexpected token? Quote your strings right—highlighters mimic parsers. Test on GitHub preview.
Platform-Specific Tips and Alternatives
GitHub: Stick to Linguist list. Stack Exchange? Auto-detects, but force ```bash.
Obsidian/Notion: ```sh works great. For blogs (Hugo/Jekyll), Prism.js—add bash to config.
No highlighter? Fallback to plain ```—better than nothing. Or embed Gists.
Edge case: Windows Git Bash? Same ```bash, but paths use /c/. Colors hold.
Sources
- GitHub Docs - Creating and Highlighting Code Blocks — Official guide to fenced blocks and language IDs: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks
- Stack Overflow - How to Highlight Bash/Shell Commands in Markdown — Community-voted solutions for bash/sh/console: https://stackoverflow.com/questions/20303826/how-to-highlight-bash-shell-commands-in-markdown
- GitHub Linguist Repository — Defines language grammars including Shell and ShellSession: https://github.com/github/linguist/linguist
- The Code Buzz - Highlight Bash Shell Code in Markdown — Examples for console vs script highlighting: https://thecodebuzz.com/highlight-bash-shell-code-in-markdown-readme-md-wiki-files/
- W3 Tutorials - Bash Shell Commands in Markdown — Editor-specific rendering and fixes: https://www.w3tutorials.net/blog/how-to-highlight-bash-shell-commands-in-markdown/
Conclusion
Nail bash syntax highlighting in Markdown with bash for scripts, console for sessions—reliable across GitHub, VS Code, and beyond. Experiment with aliases like ```sh, but test renders to dodge errors. Cleaner docs mean faster collaboration. What’s your go-to setup?