Linux—shell中$(( ))、$( )、``与${ }的区别

命令替换

在bash中,$( )` `(反引号)都是用来做命令替换的。
命令替换与变量替换差很少,都是用来重组命令行的,先完成引号里的命令行,而后将其结果替换出来,再重组成新的命令行。数组

exp 1bash

[root@localhost ~]# echo today is $(date "+%Y-%m-%d")
today is 2017-11-07
[root@localhost ~]# echo today is `date "+%Y-%m-%d"`
today is 2017-11-07

$( )与``
在操做上,这二者都是达到相应的效果,可是建议使用$( ),理由以下:学习

``很容易与''搞混乱,尤为对初学者来讲,而$( )比较直观。
最后,$( )的弊端是,并非全部的类unix系统都支持这种方式,但反引号是确定支持的。spa

exp 2.net

[root@localhost ~]#  echo Linux `echo Shell `echo today is `date "+%Y-%m-%d"```
Linux Shellecho today is 2017-11-07     #过多使用``会有问题
[root@localhost ~]# echo Linux `echo Shell $(echo today is $(date "+%Y-%m-%d"))`
Linux Shell today is 2017-11-07    ``和$()混合使用
[root@localhost ~]# echo Linux $(echo Shell $(echo today is $(date "+%Y-%m-%d")))
Linux Shell today is 2017-11-07    #多个$()同时使用也不会有问题

${ }变量替换

通常状况下,$var与${var}是没有区别的,可是用${ }会比较精确的界定变量名称的范围命令行

exp 1unix

[root@localhost ~]# A=Linux
[root@localhost ~]# echo $AB    #表示变量AB

[root@localhost ~]# echo ${A}B    #表示变量A后链接着B
LinuxB

取路径、文件名、后缀code

先赋值一个变量为一个路径,以下:
file=/dir1/dir2/dir3/my.file.txt

命令    解释    结果
${file#*/}    拿掉第一条 / 及其左边的字符串    dir1/dir2/dir3/my.file.txt
[root@localhost ~]# echo ${file#*/}
dir1/dir2/dir3/my.file.txt

${file##*/}    拿掉最后一条 / 及其左边的字符串    my.file.txt
[root@localhost ~]# echo ${file##*/}
my.file.txt

${file#*.}    拿掉第一个 . 及其左边的字符串    file.txt
[root@localhost ~]# echo ${file#*.}
file.txt

${file##*.}    拿掉最后一个 . 及其左边的字符串    txt
[root@localhost ~]# echo ${file##*.}
txt

${file%/*}    拿掉最后一条 / 及其右边的字符串    /dir1/dir2/dir3
[root@localhost ~]# echo ${file%/*}
/dir1/dir2/dir3

${file%%/*}    拿掉第一条 / 及其右边的字符串    (空值)
[root@localhost ~]# echo ${file%%/*}
(空值)

${file%.*}    拿掉最后一个 . 及其右边的字符串    /dir1/dir2/dir3/my.file
[root@localhost ~]# echo ${file%.*}
/dir1/dir2/dir3/my.file

${file%%.*}    拿掉第一个 . 及其右边的字符串    /dir1/dir2/dir3/my
[root@localhost ~]# echo ${file%%.*}
/dir1/dir2/dir3/my
记忆方法以下:

# 是去掉左边(在键盘上 # 在 $ 之左边)
% 是去掉右边(在键盘上 % 在 $ 之右边)
单一符号是最小匹配;两个符号是最大匹配
*是用来匹配不要的字符,也就是想要去掉的那部分
还有指定字符分隔号,与*配合,决定取哪部分

取子串及替换blog

命令                                    解释                              结果
${file:0:5}               提取最左边的 5 个字节                /dir1
${file:5:5}               提取第 5 个字节右边的连续 5 个字节         /dir2
${file/dir/path}            将第一个 dir 提换为 path              /path1/dir2/dir3/my.file.txt
${file//dir/path}        将所有 dir 提换为 path               /path1/path2/path3/my.file.txt
${#file}              获取变量长度                     27                            

根据状态为变量赋值ip

命令 解释 备注
${file-my.file.txt} 若 $file 没设定,则使用 my.file.txt 做传回值 空值及非空值不做处理
${file:-my.file.txt} 若 $file 没有设定或为空值,则使用 my.file.txt 做传回值 非空值时不做处理
${file+my.file.txt} 若$file 设为空值或非空值,均使用my.file.txt做传回值 没设定时不做处理
${file:+my.file.txt} 若 $file 为非空值,则使用 my.file.txt 做传回值 没设定及空值不做处理
${file=txt} 若 $file 没设定,则回传 txt ,并将 $file 赋值为 txt 空值及非空值不做处理
${file:=txt} 若 $file 没设定或空值,则回传 txt ,将 $file 赋值为txt 非空值时不做处理
${file?my.file.txt} 若 $file 没设定,则将 my.file.txt 输出至 STDERR 空值及非空值不做处理
${file:?my.file.txt} 若 $file没设定或空值,则将my.file.txt输出至STDERR 非空值时不做处理

 tips:

以上的理解在于, 你必定要分清楚 unset 与 null 及 non-null 这三种赋值状态. 通常而言, : 与 null 有关, 若不带 : 的话, null 不受影响, 若带 : 则连 null 也受影响.

数组

A="a b c def"   # 定义字符串
A=(a b c def)   # 定义字符数组
命令 解释 结果
${A[@]} 返回数组所有元素 a b c def
${A[*]} 同上 a b c def
${A[0]} 返回数组第一个元素 a
${#A[@]} 返回数组元素总个数 4
${#A[*]} 同上 4
${#A[3]} 返回第四个元素的长度,即def的长度 3
A[3]=xzy 则是将第四个组数从新定义为 xyz  

$(( ))与整数运算

 

bash中整数运算符号

符号 功能
+ - * / 分别为加、减、乘、除
% 余数运算
& | ^ ! 分别为“AND、OR、XOR、NOT”

 在 $(( )) 中的变量名称,可于其前面加 $ 符号来替换,也能够不用。

[root@localhost ~]# echo $((2*3))
6
[root@localhost ~]# a=5;b=7;c=2
[root@localhost ~]# echo $((a+b*c))
19
[root@localhost ~]# echo $(($a+$b*$c))
19

进制转换

$(( ))能够将其余进制转成十进制数显示出来。用法以下:
echo $((N#xx))
其中,N为进制,xx为该进制下某个数值,命令执行后能够获得该进制数转成十进制后的值。

[root@localhost ~]# echo $((2#110))
6
[root@localhost ~]# echo $((16#2a))
42
[root@localhost ~]# echo $((8#11))
9

(())重定义变量值

[root@localhost ~]# a=5;b=7
[root@localhost ~]# ((a++))
[root@localhost ~]# echo $a
6
[root@localhost ~]# ((a--));echo $a
5
[root@localhost ~]# ((a<b));echo $?
0
[root@localhost ~]# ((a>b));echo $?
1

 

转 :shell $(( ))、$( )、``与${ }的区别

 

 

 

***********************************************************

 学习永远不晚。——高尔基

***********************************************************

相关文章
相关标签/搜索