Fork me on GitHub

Shell Command Notes

A summary of helpful bash command sheets.

Check

  1. Check shell choices
    1
    2
    3
    4
    5
    6
    7
    $ cat /etc/shells
    /bin/sh
    /bin/bash
    /sbin/nologin
    /bin/zsh
    /bin/tcsh
    /bin/csh
  2. Check current shell path
    1
    2
    $ echo $SHELL
    /bin/bash

Variable

1
2
3
4
5
6
7
8
9
10
11
12
13

# Assign values
variable=`command`
variable=$(command)

# readonly
myUrl="http://xxx"
readonly myUrl
myUrl="http://xxx"


# delete variable
unset variable_name

Special Variable

1
2
3
# Check pid
$ echo $$
216240
Variable Meaning
$0 script name
$n the n-th parameter to scripts or functions
$# # of parameters
$* all parameters
$@ all parameters
$? Exit status of previous command (success 0, fail 1)
$$ Current process id.
  • “$*”: “$1 $2 … $n” (together)
  • “$@”: “$1” “$2” … “$n” (seperate)

Shell replacement

  1. Escape character

    1
    2
    $ echo -e "Value of a is $a \n"
    Value of a is 10\n
  2. Command replacement
    First run command, cache the result, and output.

    1
    2
    3
    4
    5
    `command`

    $ USERS=`who | wc -l`
    $ echo "Logged in user are $USERS"
    Logged in user are 1

Variable replacement

format Meaning
${var} variable value
${var:-word} if var is empty/unset, return word (donot change var)
${var:=word} if var is empty/unset, return word (set var to word)
${var:?message} if var is empty/unset, return message to stderr. Used to check whether variable can be assigned values (Exit script)
${var:+word} if var is defined, return word (donot change var)

Operator

Math

1
val=`expr 2 + 2` # -> 4

Comp op

Only support numbers, not string. Except that the string is number.

  • -eq: equal to
  • -ne: not equal to
  • -gt: greater than
  • -lt: less than
  • -ge: greater or equal to
  • -le: less or equal to
Op Meaning Example
-eq if ==, return true [ $a -eq $b ]
-ne if !=, return true [ $a -ne $b ]
-gt if >, return true [ $a -gt $b ]
-lt if <, return true [ $a -lt $b ]
-ge if >=, return true [ $a -ge $b ]
-le if <=, return true [ $a -le $b ]
1
2
3
4
5
6
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi

Bool op

Op Meaning Example
! not [ !false], return true
-o or [ $a -lt 20 -o $b -gt 100 ]
-a and [ $a -lt 20 -a $b -gt 100 ]

String op

Op Meaning Example
= string equal [ $a = $b ], return false
!= string inequal [ $a != $b ], return true
-z len(str)!=0 [ -z $a ]
str check if empty [$a] check if empty

File test op

Op Meaning Example
-b file check if it is a block device file [-b $file]
-c file check if it is a char device file [-c $file]
-d file check if it is dir [-d $file]
-r file check if it is readable [-r $file]
-w file check if it is writable [-w $file]
-x file check if it is executable [-x $file]
-s file check if file is empty [-s $file]
-e file check if file(incl. dir) exist [-e $file]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh
file="/var/www/tutorialspoint/unix/test.sh"
if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi
if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi
if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi
if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi
if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi

String

1
2
3
4
5
6
7
8
9
10
11
# 1. get string length
string="abcd"
echo ${#string} #输出 4

# 2. get substring
string="alibaba is a great company"
echo ${string:1:4} #输出liba

# 3. search substring
string="alibaba is a great company"
echo `expr index "$string" is`

Array

  1. Define array

    1
    2
    3
    4
    5
    array_name=(value0 value1 value2 value3)
    # or
    array_name[0]=value0
    array_name[1]=value1
    array_name[2]=value2
  2. Read array

    1
    2
    3
    4
    5
    ${arrray_name[index]}

    # get all elements in the array
    ${array_name[*]}
    ${array_name[@]}
  3. Get array length

    1
    2
    3
    4
    5
    6
    # get the # of elements
    length=${#array_name[@]}
    # or
    length=${#array_name[*]}
    # get the length of single element
    length_n=${#array_name[n]}

Condition

  1. If else

    1
    2
    3
    4
    if [ `expression` ]
    then
    # Statement(s) to be executed if expression is true
    fi
  2. If .. elseif … fi

1
2
3
4
5
6
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
  1. If … elseif … fi

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    if [ expression 1 ]
    then
    Statement(s) to be executed if expression 1 is true
    elif [ expression 2 ]
    then
    Statement(s) to be executed if expression 2 is true
    elif [ expression 3 ]
    then
    Statement(s) to be executed if expression 3 is true
    else
    Statement(s) to be executed if no expression is true
    fi
  2. In one line

    1
    2
    # test is used too check the condition is true or not, silimar to ([])
    if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

Case

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
casein
模式1)
command1
command2
command3
;; # break
模式2)
command1
command2
command3
;;
*)
command1
command2
command3
;;
esac

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
option="${1}"
case ${option} in
-f) FILE="${2}"
echo "File name is $FILE"
;;
-d) DIR="${2}"
echo "Dir name is $DIR"
;;
*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
esac

Result:

1
2
3
4
5
6
7
8
9
$./test.sh
test.sh: usage: [ -f filename ] | [ -d directory ]
$ ./test.sh -f index.htm
$ vi test.sh
$ ./test.sh -f index.htm
File name is index.htm
$ ./test.sh -d unix
Dir name is unix
$

For loop

1
2
3
4
5
6
7
for 变量 in 列表
do
command1
command2
...
commandN
done

While loop

1
2
3
4
while command
do
Statement(s) to be executed if command is true
done

Until loop

1
2
3
4
until command
do
Statement(s) to be executed until command is true
done

Function

1
2
3
4
5
6
function_name () {
list of commands
$1 # the first param
$2 # the second param
[ return value ]
}

Output redirection

  • stdin: 0, default read from stdin.
  • stdout: 1, default output to stdout.
  • stderr: 2, default write error message to stderr.
1
2
3
4
5
6
7
8
9
10
11
# output to file
command > file

# error redirect to file
command 2 > file

# redirect stdout and stdout
command > file 2>&1

# donot display stdout / stderr
command > /dev/null

File Operation

Compress

Extract compressed data

unzip

1
2
3
4
5
6
7
8
unzip <file>.zip
-d <exdir-path> # extract files into exdir, (default: current dir if not specified -d.)
-l # list files
-P # "extract passwd"
-t # test compressed archive data
-v # list verbosely / show version info
-o # overwrite WITHOUT prompting
-n # never overwrite existing files

Set envrionment

Set command is used to set and unset certain flags or settings within the sell environment.

1
set [option]
  • -a: It is used to mark variables that are modified or created for export.
  • -b: It is used to notify of job termination immediately.
  • -e: It is used to exit immediately if a command exits with a non-zero status.
  • -f: It is used to disable the file name generation (globbing).
  • -h: It is used to save the location of commands where they looked up.
  • -k: It is used to place all assignment arguments in the environment variable of a command, except those that precede the command name.
  • -m: It is used to enable Job control.
  • -n: It is used to read commands.
  • -o: It is used for option-name.
  • -p: It is used to disable the processing of the ‘$ENV’ file and import shell functions. It is turned on whenever the real and effective user ids do not match. Turning off this option may cause the working uid and gid to be set as the authorized uid and gid.
  • -t: It is used to exit from the command after executing one command.
  • -u: It is used to treat unset variables as an error when substituting.
  • -v: It is used to print shell input lines.
  • -x: It is used to print commands and their arguments in a sequential way (as they are executed).
  • -B: It is used to perform brace expansion by the Shell.
  • -C: It is used to disallow existing regular files to be overwritten by redirection of output.
  • -E: It is used if the ERR trap is inherited by the shell functions.
  • -H: It is used to enable style history substitution. By default, it is on when the shell is interactive.
  • -P: It is used if we do not want to follow symbolic links when executing commands.
  • -T: If this flag is set, the DEBUG trap is inherited by the shell functions.

Compress

File info

Check the size of current directories:

1
2
3
4
5
6
7
8
9
10
11
# output all file and directories in the current dir, and sort
du -h -d 1 | sort -h

# show the size of current subfiles
du -h -d 1

# show the total size of current dir
du -sh

# show the size of all subdirectoies (recursive to the max depth)
du -h