Skip to content

Instantly share code, notes, and snippets.

Last active August 25, 2018 16:28
Show Gist options
  • Save projectivemotion/dc6f9b4e6241910c3d5c026a84988b7b to your computer and use it in GitHub Desktop.
Save projectivemotion/dc6f9b4e6241910c3d5c026a84988b7b to your computer and use it in GitHub Desktop.
Cheatsheet of advanced bash commands presented in Introduction to Advanced Bash Usage - James Pannacciulli. Youtube:

Advanced Bash Usage Cheat Sheet

These commands were taking from the talk Introduction to Advanced Bash Usage by James Pannacciulli

Updated 21 Jan 2017 06:54:53

Getting help

  • type
  • help
  • apropos
  • man
  • info


  • word: sequence of characters
  • list: sequence of one or more commands or pipelines
  • name: a word that has only alphanumeric characters
  • parameter: entity that stores values.

Compound Commands

  • Iteration

    • while
    • until
    • for
    • select
  • Conditionals

    • if
    • case
  • Command Groups

    • (list) {list; })

While and Until Loops

while **list1**; do **list2**; done

until **list1**; do **list2**; done

For and Select Loops

for name in words; do list; done

	for w in w1 w2 "w3 w3-2"; do echo $w; done

for (( expr1 ; expr2 ; expr3 )); do list; done

	for (( i = 0 ; i < 5 ; i++ )) ; do echo $ioo ; done

select name in words; do list; done

	select choice in one two "three four"; do echo "$REPLY : $choice"; done


Conditional Expressions

[ *expression* ] or test *expression*

[[ expression ]]

  • word splitting is not performed
  • right side of string comparisons (==, !=) treated as
    • pattern when not quoted
    • string when quoted

Not Empty: [[ -n string ]]

String is Empty: [[ -z string ]]

String and String2 are the Same: [[ string == string2 ]]

String and String2 are not the same: [[ string != string2 ]]

String matches regular expression [[ string =~ regex ]]

File Exists: [[-e file ]]

File is a regular file: [[ -d file ]]

File is a directory: [[ -d file ]]

fd is open and refers to a terminal: [[ -t fd ]]

If Then Else

Pattern Matching

Pattern matching is used in bash for [[ ]] and case keywords, pathname expansion, and some types of parameter expansion.

  • * Matches any string, including null.
  • ? Matches any single character.
  • [character class]Matches any of the characters enclosed between []
    • [^..] Matches any character not in the class
    • [x-z] Matches the range of characters from x to z
    • [[:class:]] Matches according to the POSIX classes:
      • alnum
      • alpha
      • ascii
      • blank
      • cntrl
      • digit
      • graph
      • lower
      • print
      • punct
      • space

Conditionals: Case

Comand Groups


Command and Process Substitution


###Positional Parameters ###Special Parameters ###Variables

Parameter Expansion



###Indirection, Listing, Length

###Pattern Substitution

echo ${param/c?/T}

Left Edge

param="04: dash"
echo ${param/#* /}

Right Edge

echo ${param/%r/t}

Indexed Arrays

Not Available in POSIX Shell

Arithmetic Expansion

##Brace Expansion

$ echo he{ll,lp}
hell help

$ echo ki{ss,ll}ing
kissing killing

$ echo bash{,e{d,s},ful{,ly,ness},ing}
bash bashed bashes bashful bashfully bashfulness bashing

$ echo foo{,}
foo foo

$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z

$ echo {1..2}
1 2

$ echo {1..1}

$ echo {1..0}
1 0

$ echo {9..0..2}
9 7 5 3 1


##Session Portability

Cheatsheet is incomplete at this time.

Copy link

Great job! Here is the PDF with slides from original presentation:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment