Skip to content

Instantly share code, notes, and snippets.

@netrunn3r
Last active December 7, 2017 12:38
Show Gist options
  • Save netrunn3r/0ce8df6ec0717c2bed48f2056c016b7f to your computer and use it in GitHub Desktop.
Save netrunn3r/0ce8df6ec0717c2bed48f2056c016b7f to your computer and use it in GitHub Desktop.
Bash cheatsheet

Arrays

distro=("redhat" "debian" "gentoo")
echo ${distro[0]} 
echo ${distro[2]}  # will print gentoo
echo ${#distro[@]} # print array size, here 3

tLen=${#distro[@]} 
for (( i=0; i<${tLen}; i++ ));
do
  echo ${distro[$i]}
done

Basic

foo="asdf"
echo${#foo} # 4 - lenght of foo

Incrementation

i=0
((i++)) # i=1
((i+=3)) # i=4

Command substitution

$(cmd) # POSIX
`cmd` # more or less obsolete for Bash
$((cmd)) # arithmetic expansion
$( (cmd) ) # explicit subshell (cmd) inside the command substitution $( )

Nesting

echo `echo `ls``      # INCORRECT
echo `echo \`ls\``    # CORRECT
echo $(echo $(ls))    # CORRECT
echo "$(echo "$(ls)")" # nested double-quotes

Condition execution

git commit && git push
git commit || echo "Commit failed"

Parameter expansions

Substitution

STR="/path/to/foo.cpp"
# ${FOO%suffix}	Remove first suffix
echo ${STR%.cpp}    # /path/to/foo
echo ${STR%.cpp}.o  # /path/to/foo.o
# ${FOO%%suffix}	Remove long suffix

# ${FOO#prefix}	Remove prefix
echo ${STR#*/}      # path/to/foo.cpp
# ${FOO##prefix}	Remove long prefix
echo ${STR##*.}     # cpp (extension)
echo ${STR##*/}     # foo.cpp (basepath)
# ${FOO/from/to}	Replace first match
echo ${STR/foo/bar} # /path/to/bar.cpp
name="Johnny"
echo ${name/n/N}    #=> "JohNny" 
# ${FOO//from/to}	Replace all
echo ${name/n/N}    #=> "JohNNy" 
# ${FOO/%from/to}	Replace suffix
BASE=${STR##*/}   #=> "foo.cpp" (basepath)
DIR=${SRC%$BASE}  #=> "/path/to" (dirpath)
# ${FOO/#from/to}	Replace prefix

Substrings

name="Johnny"
# ${FOO:0:2}	Substring (position, length)
echo ${name:0:2}    #=> "Jo" 
echo ${name:0:-2}   #=> "John" 

Default values

# ${FOO:-val}	return $FOO, or val if $FOO not set
unset foo
echo ${food:-Cake}  #=> "Cake"
echo $food          #=> ""
food="Pie"
echo ${food:-Cake}  #=> "Pie"
# ${FOO:=val}	Set $FOO to val if $FOO not set
unset foo
echo ${food:=Cake}  #=> "Cake"
echo $food          #=> "Cake"
food="Pie"
echo ${food:=Cake}  #=> "Pie"
# ${FOO:+val}	return val if $FOO is set
unset foo
echo ${food:+Cake}  #=> ""
echo $food          #=> ""
food="Pie"
echo ${food:+Cake}  #=> "Cake"
# ${FOO:?message}	Show error message and exit if $FOO is not set

Conditions

This is bash, where you can use (( )) and >:

if (( a > b )); then
    ...
fi

This is POSIX shell, where you use [ ] and cannot use >, but -gt:

if [ "$a" -gt "$b" ]; then
    ...
fi

Use cases

Ask user yes or no

while true; do
    read -p "Do you wish to install this program?" yn
    case $yn in
        [Yy]* ) make install; break;; # also Y|y or Y|y|Yes|yes
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

Pause running command

CTRL + Z It pause running command and put it to background. With jobs you can see cmd in backgrounds. To resume cmd in background type bg or to foreground with fg. If you have more than one jobs you can use bg %N or fg %N.

Choose N random lines from file

shuf -n N input > output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment