note/sh-builtin

Shell (Bash) Cheatsheet

structure

a; b run a, then run b
a && b run a, if successed, then run b
a || b run b, if failed, then run b

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

can be replaced with:

[[ -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, it 'll return error code depending on the <expression>.

if [[ <expression> ]]; then
    <list>
fi
  • -a <file>: true when file exist
  • -v <var>: true when variable is set
  • str1 <|>|==|=|!= str2: compare strings lexicographically
  • str1 <OP> str2: compare numeriacally, 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=233 assign $var with '233', note that there should be no space surrounding =
echo $var get 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 clipping

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)
echo $arr bash: first, zsh: first second third
arr[1]=2nd
echo ${arr[1]} 2nd
${arr[@]} first 2nd third
${!arr[@]} 0 1 2
${#arr[@]} 3

quote

  • "": 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.txt stdout -> file
a >> file.txt append stdout to a file
`a b`
a < file.txt file -> stdin
a `b` stdout -> args
a << "some string" -> stdin
a << delimiter
    This is here-doc.
    You can write anything here
    as the stdin of 'a',
    until your 'delimiter' appears.
delimiter

built-in command

echo <args> args -> stdout
pwd show 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 accessable by other programs by default
set -- "qwe" "rty" let $1: "qwe", $2: "rty"
read [-p "Your name? "] var read some val to a variable
<var>=<value> <program> run a program under an environ var
getopts <optstr> <var> see Getopts

local variable

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

true exit code 0
false exit code 1

others

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

About Me