How can I view a reference or cheat sheet of the basic bash syntax, in the linux terminal?

  • man bash is the canonical Bash reference.
  • help help gives you help on the help Bash builtin, and how it gives you short help messages about other Bash built-ins, such as help for.
  • Greg's Wiki is the best place to find to-the-point, terse, and sometimes even entertaining reference material for pretty much every aspect of Bash.
  • If you want a quick way to look up syntax, you could version control .bash_history. There are just too many commands, different parameter syntaxes, sed and awk line noise and other stuff to ever remember all of it, so it's nice to have a "well groomed" personal reference with tags to look up in.

Since there are plenty of such things online, here's a suggestion for you; I use this for all kinds of things.

Create a directory $HOME/notes. Add a subfolder, bash. If you don't have a $HOME/bin, create one, add it to your path using whatever method you prefer (for example: in ~/.bashrc). Then put a simple script in there, cmdref.sh:

#!/bin/bash

if [ -z "$CMREF_DIR" ]; then
        CMREF_DIR="$HOME/notes"
fi

cat "$CMREF_DIR/${1}/${2}.txt"

Symlink it for convenience, ln -s ~/bin/cmdref.sh ~/bin/cmdref; I like to do this so you can edit the cmdref.sh source and an editor will still recognize the filetype by the suffix.

Copy paste whatever into various files in ~/notes/bash -- e.g., you could have a file arrays.txt, loops.txt, etc. As long as this is just for your own personal use, you're not violating copyright.

You can now use that from the command line, e.g.: cmdref bash arrays and that cheat sheet will be printed to the console. Note although the files are .txt (again, using appropriate suffixes is useful for editors, file browsers, etc.) you should not use cmdref bash arrays.txt since it's appended in cmdref.sh (the only thing the script does is convert paths and add the suffix).


man bash covers every aspect of bash syntax, e.g. for if statements, string is zero and non-existent files, just search ( hit the / key and enter the following ) for CONDITIONAL EXPRESSIONS.

Tags:

Bash