Linux Shell 脚本中 ${} 用法
在Linux 的 Shell 脚本中,${} 通常被用来做变量替换。
1、常规用法
例如在 Shell 脚本中设置:
1 | A=B |
则:
1 | echo $AB |
表示输出名为AB的变量表示的值,并不是变量A的值再加上B。
1 | echo ${A}B |
表示输出名为A的变量表示的值,并在后面加上B,即输出
BB
即用${}会比较精确的界定变量名称的范围。
2、变量的赋值
在${}中可以使用一些简单的语法,完成变量的替换工作。
假设定义变量
1 | file=/dir1/dir2/dir3/myfile.txt |
2.1 -(减号)
${file-myfile.txt}
表示如果file没有定义,则使用myfile.txt 作返回值(如果file已定义,但为空值或非空值时不作处理)。
例如,假设脚本文件tt.sh 内容如下:
1 2 3 4 5 | echo ${file-myfile.txt} file= echo ${file-myfile.txt} file=/dir1/dir2/dir3/myfile.txt echo ${file-myfile.txt} |
执行./tt.sh输出如下:
1 2 3 4 | # ./tt.sh myfile.txt /dir1/dir2/dir3/myfile.txt |
2.2 :-(冒号与减号)
表示如果 $file 没有定义或被设置为空值,则使用myfile.txt作为返回值。(非空值时不作处理,返回 $file 表示的值)
例如,假设脚本文件tt.sh 内容如下:
1 2 3 4 5 | echo ${file:-myfile.txt} file= echo ${file:-myfile.txt} file=/dir1/dir2/dir3/myfile.txt echo ${file:-myfile.txt} |
执行./tt.sh 提到内容如下:
1 2 3 4 | # ./tt.sh myfile.txt myfile.txt /dir1/dir2/dir3/myfile.txt |
2.3 +(加号)
表示如果 $file 设置为空值或非空值,均使用myfile.txt 作为返回值(如果没定义 $file 则不处理,直接返回空)。
例如,假设脚本文件 tt.sh 内容如下:
1 2 3 4 5 | echo ${file+myfile.txt} file= echo ${file+myfile.txt} file=/dir1/dir2/dir3/myfile.txt echo ${file+myfile.txt} |
执行 ./tt.sh 得到内容如下:
1 2 3 4 | # ./tt.sh myfile.txt myfile.txt |
2.4 :+(冒号与加号)
表示如果 $file 为非空值,则使用myfile.txt 作为返回值(如果没有定义 $file 或者其为空值时不作处理)。
例如,假设脚本文件 tt.sh 内容如下:
1 2 3 4 5 | echo ${file:+myfile.txt} file= echo ${file:+myfile.txt} file=/dir1/dir2/dir3/myfile.txt echo ${file:+myfile.txt} |
执行 ./tt.sh 得到内容如下:
1 2 3 4 | # ./tt.sh myfile.txt |
2.5 =(等号)
表示如果 $file 没有定义,则使用 myfile.txt 作为返回值,同时将 $file 赋值为 myfile.txt(如果 $file 已定义,不管是空值还是非宿舍,则直接返回 $file 表示的数据)。
例如,假设脚本文件 tt.sh 内容如下:
1 2 3 4 5 | echo ${file=myfile.txt} file= echo ${file=myfile.txt} file=/dir1/dir2/dir3/myfile.txt echo ${file=myfile.txt} |
执行 ./tt.sh 得到如下内容:
1 2 3 4 | # ./tt.sh myfile.txt /dir1/dir2/dir3/myfile.txt |
2.6 :=(冒号与等号)
表示如果 $file 没有定义,或被设置为空值,则使用 myfile.txt 作为返回值,同时将 $file 的值设置为 myfile.txt(当 $file 非空时不作处理,直接返回 $file 表示的数据)。
例如,假设脚本文件 tt.sh 内容如下:
1 2 3 4 5 | echo ${file:=myfile.txt} file= echo ${file:=myfile.txt} file=/dir1/dir2/dir3/myfile.txt echo ${file:=myfile.txt} |
执行 ./tt.sh 得到内容如下:
1 2 3 4 | # ./tt.sh myfile.txt myfile.txt /dir1/dir2/dir3/myfile.txt |
2.7 ?(问号)
表示如果没有定义 $file,则将myfile.txt 输出到 stderr,同时脚本不再往下执行;
如果已定义了 $file,则直接返回其表示的值,不管是空值还是非空值。
例如,假设脚本文件 tt.sh 内容如下:
1 2 3 4 5 | echo ${file?myfile.txt} file= echo ${file?myfile.txt} file=/dir1/dir2/dir3/myfile.txt echo ${file?myfile.txt} |
执行 ./tt.sh 得到内容如下:
1 2 | # ./tt.sh ./tt.sh: line 1: file: myfile.txt |
将 tt.sh 的第一行注释掉之后,再次执行 ./tt.sh 得到内容如下:
1 2 3 | # ./tt.sh /dir1/dir2/dir3/myfile.txt |
2.8 :?(冒号和问号)
表示如果没有定义 $file,或者 $file 为空值,则将 myfile.txt 输出到stderr,同时不再往下执行;
如果 $file 已定义且为非空值,则直接返回 $file 表示的数据。
例如,假设脚本文件 tt.sh 的内容如下:
1 2 3 4 5 | echo ${file:?myfile.txt} file= echo ${file:?myfile.txt} file=/dir1/dir2/dir3/myfile.txt echo ${file:?myfile.txt} |
执行 ./tt.sh 后得到内容如下:
1 2 | # ./tt.sh ./tt.sh: line 1: file: myfile.txt |
将第一行注释掉之后再次执行 ./tt.sh 后得到内容如下:
1 2 | # ./tt.sh ./tt.sh: line 3: file: myfile.txt |
将第三行也注释掉之后,再次执行 ./tt.sh,得到内容如下:
1 2 | # ./tt.sh /dir1/dir2/dir3/myfile.txt |
3、变量所表示的字符串的长度
1 | ${#str} 输出str表示的字符串的长度 |
4、字符串的截取
4.1
1 | ${file#*/} |
去掉从左数第一个 “/” 及其左边的所有字符,其中 “/” 也可以替换为其他字符。
4.2
1 | ${file##*/} |
去掉从左数最后一个 “/” 及其左边的所有字符,其中 “/” 也可以替换为其他字符。
4.3
1 | ${file%/*} |
去掉从左数最后一个 “/” 及其右边的所有字符,其中 “/” 也可以替换为其他字符。
4.4
${file%%/*}
去掉从左数第一个”/”及其右边的所有字符,其中 “/” 也可以替换为其他字符。
5、字符串的提取
1 | ${file:m:n} |
提取从第 m 个字符开始的 n 个字符。
如
1 2 | ${file:0:5} 表示提取字符串的前5个字符。 ${file:1:${#file}} 表示提取字符串除第 1 个字符之外的所有字符。 |
6、字符串替换
6.1
1 | ${file/dir/path} |
表示将字符串file 中出现的第一个 dir 替换为 path。
6.2
1 | ${file//dir/path} |
表示将字符串 file 中出现的所有 dir 替换为 path。
7、数组运算
假设定义数组ARR如下:
1 | ARR=(a b c def) |
则:
1 2 3 4 | ${ARR[@]} 或 ${ARR[*]} 可得到数组的全部元素 ${ARR[0]} 可得到数组的第0个元素,${ARR[1]} 可得到数组的第 1 个元素,依次类推。 ${#ARR[@]} 或 ${#ARR[*]} 可以得到数组中元素的数量。 ${#ARR[1]} 可得到数组的第 0 个元素的长度,${#ARR[1]} 可得到数组的第 1 个元素的长度,依次类推。 |
参考:
https://www.jianshu.com/p/f7234405eb79
https://www.cnblogs.com/cornerxin/p/9105612.html
————————————————————
原创文章,转载请注明: 转载自孙希栋的博客
本文链接地址: 《Linux Shell 脚本中 ${} 用法》