a=1
b=2
c=3
echo $a
unset c
str1="hello"
echo "字符串长度:${#str1}"
echo '单引号内不能有变量:${#str1}'
echo "执行文件名:$0"
echo "第一个参数为:$1"
array1=(1 2 3)
array1[3]=4
echo ${array1[2]}
echo "数组长度:${#array1[@]}"
echo "所有元素(1个字符串):${array1[*]}"
echo "所有元素(n个):${array1[@]}"
declare -A array2=(["aa"]="11" ["bb"]="22")
echo ${array2["aa"]}
echo "数组的键:${!array2[@]}"
val1=`expr $a + $b`
echo "ab之和:$val1"
read name
echo "遇\n换行"
echo -e "hello,\n"
echo -e "\"world\""
echo "遇\c不换行"
echo -e "hello,\c"
echo -e "\"world\""
echo "It is a test" > myfile
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.18
a=3
b=4
if test a gt b
then
echo "左数更大"
else
echo "右数更大"
fi
res1=$[a+b]
echo "ab之和:$res1"
if [$a -eq $b]
then
echo "ab相等"
elif (($a > $b))
then
echo "a大于b"
else
echo "a小于b"
fi
for var1 in item1 item2 ... itemN
do
command1
command2
...
commandN
done
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
while read FILM
do
echo "$FILM 是一个好网站"
done
while :
do
command
done
for (( ; ; ))
until condition
do
command
done
case 值 in
值1)
command1
;;
值2)
command2
commandn
;;
esac
[ function ] funname [()]
{
action;
[return int;]
}
funWithReturn(){
echo "该函数将输入的两个数字相加"
echo "输入第一个数字: "
read aNum
echo "输入第二个数字: "
read anotherNum
echo "两个数字分别为 $aNum 和 $anotherNum 。"
return $(($aNum+$anotherNum))
}
funWithReturn
echo "输入的两个数字之和为 $? !"
echo "第二次获取会失效 $? "
funWithParam(){
echo "第一个参数为 $1 !"
echo "第二个参数为 $2 !"
echo "第十个参数为 ${10} !"
echo "第十一个参数为 ${11} !"
echo "参数总数有 $# 个!"
echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
command1 > file1
echo "hello" >> file1
cat users
command1 < file1
command1 < infile > outfile
command << delimiter
document
delimiter
command > /dev/null
command > /dev/null 2>&1
. filename
source filename