Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaoyuT/c62390b58416589f79784a674faca3de to your computer and use it in GitHub Desktop.
Save DaoyuT/c62390b58416589f79784a674faca3de to your computer and use it in GitHub Desktop.
Cheatsheet of advanced bash commands presented in Introduction to Advanced Bash Usage - James Pannacciulli. Youtube: https://youtu.be/uqHjc7hlqd0

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

Definitions

  • 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

Tests

https://youtu.be/uqHjc7hlqd0?t=11m56s

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

https://youtu.be/uqHjc7hlqd0?t=14m23s

Pattern Matching

https://youtu.be/uqHjc7hlqd0?t=15m3s

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

https://youtu.be/uqHjc7hlqd0?t=15m49s

Comand Groups

https://youtu.be/uqHjc7hlqd0?t=16m59s

Redirection

https://youtu.be/uqHjc7hlqd0?t=21m36s

Command and Process Substitution

https://youtu.be/uqHjc7hlqd0?t=22m20s

Parameters

https://youtu.be/uqHjc7hlqd0?t=23m9s

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

Parameter Expansion

###Conditionals https://youtu.be/uqHjc7hlqd0?t=24m5s

###Substrings https://youtu.be/uqHjc7hlqd0?t=26m57s

###Indirection, Listing, Length https://youtu.be/uqHjc7hlqd0?t=29m1s

###Pattern Substitution

https://youtu.be/uqHjc7hlqd0?t=30m17s

param=racecar
echo ${param/c?/T}
raTcar

Left Edge

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

Right Edge

param=racecar
echo ${param/%r/t}
racecat

Indexed Arrays

Not Available in POSIX Shell https://youtu.be/uqHjc7hlqd0?t=31m3s

Arithmetic Expansion

https://youtu.be/uqHjc7hlqd0?t=33m47s

##Brace Expansion https://youtu.be/uqHjc7hlqd0?t=35m26s

$ 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}
1

$ echo {1..0}
1 0

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

##Functions https://youtu.be/uqHjc7hlqd0?t=37m49s

##Session Portability https://youtu.be/uqHjc7hlqd0?t=40m35s

Cheatsheet is incomplete at this time.

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