note/sh-builtin

Shell (Bash) Cheatsheet

Structures

a; brun a, then run b
a && brun a, if succeeded, then run b
a || brun b, if failed, then run b

if [[ ! -f .bashrc ]]; then
    echo "conf is not found"
    touch .bashrc
fi

is equivalent to:

[[ -f .bashrc]] || {
    echo "conf is not found"
    touch .bashrc
}

function

f() {
    echo $1
}
  • $1, $2 etc is the 1st, 2nd etc argument
  • $# is the amount of arguments
  • $* / [email protected] is an array of all arguments

case

case $var in
    1)
        echo one;;
    2)
        echo two;;
    *)
        echo other;;
esac

for

for var in (one two three)
do
    echo $var
done
for (( i=0; i<10; i++ ))
do
    echo $i
done

if

if <list0>; then
    <list1>
else
    <list2>
fi

Run <list1> when <list0> exit with code 0, elsewhere run <list2>.

Brackets

/ [ ]

(Built-in command) Conditional brackets, you can think the left bracket as a program. The “program” returns an error code depending on the <expression>. [[ is an enhanced version of [ which may not be available in lower versions of bash.

if [[ <expression> ]]; then
    <list>
fi

Here's an incomplete list of <expression>:

  • -a <file>: true when file exist
  • -v <var>: true when variable is set
  • str1 <|>|==|=|!= str2: compare strings lexicographically
  • str1 <OP> str2: compare numerically, where <OP> is:
    • -eq, -ne: equal to, not equal to
    • -lt, -le: less than, less than or equal to
    • -gt, -ge: greater than, greater than or equal to

(( ))

(Built-in command) Exit with code 0 when its a non-zero number.

(( 1 )) # code 0
(( 0 )) # code 1
(( nan )) # code 1

$

(Keyword) Math brackets.

echo $[ 9 * 9 ] # 81

$( )

(Keyword) Equivalent to `` .

Variable

There are two types of variables, string and array.

var=233assign $var with '233', note that there should be no space surrounding =
echo $varget the value of $var
echo ${#var}len() of $var

Tip: var=string with-space is invalid, you will execute command with-space with $var set to string, use quotes in these cases.

String clipping

str='nvida f*ck you'
echo ${str:6:4} # f*ck

String splitting

var="123/456/789"
echo ${var#*/}    # 456/489
echo ${var##*/}   # 789
echo ${var%/*}    # 123/456
echo ${var%%/*}   # 123
|->|
123/456/789
|----->|

       |<-|
123/456/789
   |<-----|

Array

arr=(first second third)assign $arr with an array
echo $arrbash: first, zsh: first second third
arr[1]=2ndarray item assignment, the suffix counts from 0, negative: reverse counting
echo ${arr[1]}2ndget an item
${arr[@]}first 2nd thirdget all items
${!arr[@]}0 1 2
${#arr[@]}3len of array

Quotes

  • "": parse variables and escape characters in it
  • '': what you see is what you get
echo ~    # /home/genel
echo "~"  # ~
echo *    # documents downloads pictures music
echo "*"  # *

Pipe

a > file.txtstdout -> file
a 2> file2.txtstderr -> file
a >> file.txtappend stdout to a file
a | bstdout of a -> stdin of b
a < file.txtfile -> stdin
a `b`stdout -> args
a << "some string""some string" -> stdin
a << delimiter
    This is here-doc.
    You can write anything here
    as the stdin of 'a',
    until your 'delimiter' appears.
delimiter

For some cases (e.g. in scripts), you don't want program to print anything to the session. Redirect the stdout and stderr of the program to /dev/null.

Built-in command

echo <args>args -> stdout
pwdshow current working directory
source <file>run a shell script in current shell process (preserve states after the script exit)
eval <command>run a command in current shell process

Variable

export <var>[=<value>]make a variable environ, vars are not accessible by other programs by default
set -- "qwe" "rty"let $1: "qwe", $2: "rty"
read [-p "Your name? "] varread some val to a variable
<var>=<value> <program>run a program under an environ var
getopts <optstr> <var>see Getopts

Variables are global by default, use a local keyword to limit their scopes.

f() { a=2333; }
f
echo $a    # 2333
g() { local b=2333; }
g
echo $b    # ''

Process

fg <process>foreground
bg <process>background
exec <prog>run a program to replace current shell process

Other commands

trueexit code 0
falseexit code 1

Others

<(a)"file" name of a process
a &run a in the background

About Me