shell 脚本中的 sh 和 bash 是有区别的
shell 脚本中的 sh 和 bash 是有区别的
这两天在学习 shell 脚本相关知识,才知道 sh
和 bash
是不一样的。
bash
是 sh
的超集。bash
包含 sh
。
比如 bash 中能用的 [[ ]]
和 数组 array("a" "b")
等,在 sh 中都不可用。
BASH 写法
if [[ $a -gt $b ]] # 可以写成这样
then
echo "${a} is bigger"
fi
SH 写法
if $a -gt $b
then
echo "${a} is bigger"
fi
sh 中没有数组的概念
BASH
array_ch=("一" "二" "三" "四")
array=("one" "two" "three" "four")
for index in ${!array[@]}; do
echo "${array[$index]} is ${array_ch[$index]}."
done
# 结果
# one is 一
# two is 二
# three is 三
# four is 四
SH 不支持数组,就没有索引,想实现相同的功能,就需要想一下怎么写了
for name in "one" "two" "three" "four" ; do
echo "${name}"
done
# 结果
# one two three four