string="abcdefg" #get the length of the string echo ${#string} #get the four chars of string start from index 1, Attention: index start from 0 echo ${string:1:4} #get "unoo" var="This is a string" echo ${string#*s} #s is a string echo ${string##*s} #tring echo ${string%s*} #This is a echo ${string%%s*} #Thi
Search the string
1 2 3
string="abcdefghijkl" #print the first index of char 'i' echo `expr index "${string} i"`
array_value=${array_name1[n]} #get all elements of the array echo ${array_name1[@]} #get the length of the array length=${#array_name[@]} length=${#array_name[*]} #get the length of the first element of array length=${#array_name[0]}
IV. Shell Comment
1 2 3 4 5 6 7 8
# This is a comment line
:<<EOF This is a triple-line comment EOF # 'EOF' here can be replaced by any other symbol (like ', !)
V. Shell parameter
1 2 3 4 5 6 7 8 9
$ bash ./shell.sh 1 2 3 $0 : the file name of shell $1 : 1 $2 : 2 $3 : 3 $# : 3 # the number the parameter $* : show all the parameters as a string $@ : some as $* $? : show the status of the last command
VI. Shell Operator
1 2 3 4 5 6 7 8 9 10 11 12 13
var=`expr 2 + 2` var=$(expr 2 + 2) var=`expr 2 \* 3` a=10 b=20 if [ $a != $b -a $b -gt $(expr $a - $b) ];then echo "hi" fi string="" if [ -n "$string" ] echo "The length of the string is 0" fi [[ && || > < ]]