basic

1
2
3
4
5
6
7
8
9
10
11
12
printf "%-5s %-10s %-4s\n" No Name Mark


pgrep bash # get pid of bash
cat /proc/pid/environ # get env var


var=value # assign value
var = value # check equal


length=${#var}

calculate

1
2
3
4
5
6
7
8
9
10
11
12
# calculate only for int
let no1++
$[ $no1 + 1 ]
$(( $no1 +5 ))
$(expr $no1 + 5 )


# calculate for float
echo "4 * 0.56" | bc # 2.24
echo "scale=3;3/8" | bc # .375
echo "obase=2;100" | bc # 1100100
echo "obase=10;ibase=2;100" | bc # 4

std

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# std
cmd 2>stderr.txt 1>stdout.txt


cmd 2>&1 output.txt
cmd &> output.txt


cat a* 2> /dev/null


# tee copy
cat a* | tee out.txt | cat -n # only stdin, no stderr


# muti-line stdin
cat <<EOF>log.txt
test1
test2
EOF

array

1
2
3
4
5
6
7
8
9
10
11
12
13
# array
array_var=(1 2 3 4 5 6)
echo ${array_var[0]}
echo ${array_var[*]} # all
echo ${array_var[@]} # all
echo ${#array_var[@]} # len


declare -A ass_array
ass_array=([apple]=100 [orange]=150)
echo "${ass_array[apple]}"
echo "${!ass_array[*]}" # key
echo "${#ass_array[*]}" # len

date

1
2
3
4
5
date

date +%s

date --date "Jan 20 2001" +%A