| title | Bash Shell 学习笔记 | |||
|---|---|---|---|---|
| date | 2020-06-16 16:44:01 -0700 | |||
| update | 2021-03-20 14:47:39 -0700 | |||
| categories | 笔记 | |||
| tags |
|
这里记录了一些个人学习 Bash Shell 时遇到的一些问题, 可以作为避免踩坑和速查手册. 如有疑问欢迎留言.
字符串可以用单引号,也可以用双引号,也可以不用引号。
单引号里任何字符都会原样输出,变量也是无效的,也无法对单引号进行转义。
str='this is a string'双引号就可以随意的进行转义啦!
name="mutoe"
greeting="hello, $name !" # => hello, mutoe !
greeting="hello, "$name" !" # => hello, mutoe !
greeting="hello, ${name}!" # => hello, mutoe!
quote_greeting="hello, \"$name\" !" # => hello, "mutoe" !str='abcd'
echo ${#abcd} # => 4str='abcdefg'
echo ${string:1:4} # => bcdestr="a,b,c,d"
arr=$(echo $str | tr ";" "\n")
for item in $arr
do
echo $item
done数组使用小括号定义
# 声明数组
array=(1 2 a b "foo")
array[10]=bar
# 读取数组
echo ${array[3]} # => b
echo ${array[9]} # =>
echo ${array[10]} # => bar
echo ${array[*]} # => 1 2 a b foo bar
echo ${array[@]} # => 1 2 a b foo bar
# 读取数组长度
echo ${#array[*]} # => 6
# 读取字符串元素长度
echo ${#array[10]} # => 3
# 删除元素
unset array[1]
echo ${array[*]} # => 1 a b foo bar
echo ${#array[*]} # => 5
# 拼接数组
new_arr=(0 ${array[*]} z)
echo ${new_arr[*]} # => 0 1 a b foo bar z
echo ${new_arr[6]} # => z
# 删除数组
unset new_arr
echo $new_arr # => Bash shell 中可以使用字符串作为数组的下标,类似 Map 对象
declare -A color
color["red"]="#ff0000"
color["green"]="#00ff00"
color["blue"]="#0000ff"
echo $colorif condition
then
...
elif condition2
then
...
else
...
fi实际上,if 检测的是一条命令的退出状态。
Example
a=$[2*3]
b=$[1+5]
if [ $a == $b ]; then
echo "a == b"
ficase $var in
1)
command1
command2
;; # break (required)
$const|2) # or
command1
command2
;;
*) # default
command1
command2
;;
esac以 # 开始的部分就是注释, sh里没有多行注释,只能每行加一个 # 号