# double quote
name="hel\"lo"
# single quote
name='hello'
name="hello${str1}"
name="hello"${str2}"world"
name="hello${str}world"
# for loop
for file in `ls`; do
echo ${file}
done
# count string total char
echo ${#str1}
# string slice
echo ${str:4:2}
echo "hello" "world"
# find index
echo `expr index ${str} sub`
# array
arr=("he" "llo" "wo" "rld")
# asign to array
arr[0]="yesido"
# no boundry limit
arr[90]="lastone"
echo ${arr[90]}
# get all
echo ${arr[@]}
# count array total element
length=${#arr[@]}
# count array total element
echo ${#arr[*]}
# count string total char
echo ${#arr[2]}
# comment
# this is comment
# filename of this sh
$0
# nth argument
$1 $2 $3 $4
# count total argument
$#
# current process id
$$
# last deamon process id
$!
# last process return value
$?
# all arguments in a string
$*
# all arguments in a array
"$*"
arr2=(a b ccc DDD "yes" 'sss' 12)
echo ${arr2[@]}
# space!!
res=`expr 2 + 2`
res=`expr 7 \* 89`
# + - \* / % == !=
# space!!
if [ ${a} == ${b} ]
then
#
fi
# compare
# -eq -nq -gt -lt -ge -le
# logical
# ! -o -a
# logical
# && ||
# check
# -d -b -c -f -p -r -w -x -s -e
if [ -e "/etc/apt/sources.list" ]
path="/etc/src"
if [ -d $path ]
echo "this is string"
echo this is string
# read stdin to string
read line
echo "new line is ${line}"
# -e enable escape
echo -e "firstline\nsecondline"
# \c disable change line
echo -e "lastline\c"
# > redirect stdout, < redirect stdin
echo "anything" > filename.suffix
# tee redirect to file also to stdout
echo "anything" | tee filename.suffix
# single quote, disable variable parse and escape
echo 'something&{name}$n\ame'
echo `date`
# printf usage
# printf format-string arguments
printf "hello world\n"
printf "%-10s%5s\n" "hello" "worlds"
# %-10s %4.2f %2s %d
# \a \c \t \n \\ \v \f \b
# flow control
if test 1 == 2
if test $num1 -eq ${num2}
if test -d file
if [ 1 == 2 ]
then
echo 1
echo 2
echo "eq"
fi
# single line, ;
if test 1 -eq 2; then echo "eq"; fi
# if then elif else fi
if [ $a -gt $b ]
then
echo $a
elif [ $a -ge 7 ]
then
echo 7
else
echo "none"
fi
echo $[ 2+ 4]
echo `expr 2 + 4`
# for in do done
for num in 1 2 3 4 5; do
echo $num
done
# while do done
while test 1 == 2; do echo hello; done;
i=0
while test $i -le 5
do
echo $i
let "i++"
done
# forever loop
while :
do
echo "loop forever"
done
while true
do
echo "loop forever"
done
# util do done
util test 1 == 2
do
echo "1 will never equal to 2"
done
# case
case $value in
1) echo 1
;;
"hello") echo "he"; echo "llo"
;;
*) echo "handle default"
;;
esac
# break continue -> for while-do util-do
# function definition
func_name(){
echo $1 $2 $3 $# $*
return 255
}
# call function
func_name 1 2 3 4 5
# get function result
echo $?
echo "hello" > filename
# redirect to file, add
echo "add" >> filename
# > >> < << >& <&
# 0 1 2 stdin stdout stderr
# command < input > output
# command > file 2>1
# command > file 2>&1
wc -l << EOF
"doc"
EOF
# discard stdout
echo "anything" > /dev/null
# marge stderr to stdout, then discard stdout
echo "any stdout any error" > /dev/null 2>&1
# include external sh
. /path/to/external/sh