linux $()

Command substitution

In bash, $( )and (backquotes) are used for command substitution. Command substitution is similar to variable substitution. Both are used to reorganize the command line. First complete the command line in quotation marks, then replace the result, and then reassemble the new command line.

exp 1

[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

$() and `` In operation, both of them achieve the corresponding effect, but it is recommended to use $() for the following reasons:

`` is easy to confuse with ”, especially for beginners, and $() is more intuitive. Finally, the disadvantage of $() is that not all Unix-like systems support this method, but backticks are definitely supported.

exp 2

[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    #多个$()同时使用也不会有问题

${} variable substitution

In general, there is no difference between $var and ${var}, but using ${} will more accurately define the scope of the variable name

exp 1

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

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

Take the path, file name, suffix

Copy 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
记忆方法如下:

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

Copy code

Take substring and replace

命令                                    解释                              结果
${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                            

Assign values ​​to variables based on state

command Explanation Remarks
${file-my.file.txt} If $file is not set, use my.file.txt as the return value Null and non-empty values ​​are not processed
${file:-my.file.txt} If $file is not set or is empty, use my.file.txt as the return value No processing when non-empty value
${file+my.file.txt} If $file is set to empty or non-empty, use my.file.txt as the return value No processing if not set
${file:+my.file.txt} If $file is non-empty, use my.file.txt as the return value No setting and empty value will not be processed
${file=txt} If $file is not set, return txt and assign $file to txt Null and non-empty values ​​are not processed
${file:=txt} If $file is not set or is empty, return txt and assign $file to txt No processing when non-empty value
${file?my.file.txt} If $file is not set, output my.file.txt to STDERR Null and non-empty values ​​are not processed
${file:?my.file.txt} If $file is not set or empty, output my.file.txt to STDERR No processing when non-empty value

tips:

The above understanding is that you must clearly distinguish the three assignment states of unset, null and non-null. Generally speaking,: is related to null. If you don’t include:, null is not affected. If you include: then even null is also Affected.

Array

A="a b c def"   # 定义字符串
A=(a b c def)   # 定义字符数组
command Explanation result
${A[@]} Return all elements of the array a b c def
${A[*]} Same as above a b c def
${A[0]} Returns the first element of the array a
${#A[@]} Returns the total number of array elements 4
${#A[*]} Same as above 4
${#A[3]} Returns the length of the fourth element, which is the length of def 3
A [3] = xzy Is to redefine the fourth group number as xyz

$(( )) and integer operations

Integer arithmetic symbols in bash

symbol Features
+ - * / Respectively add, subtract, multiply, and divide
% Remainder operation
& | ^ ! Respectively "AND, OR, XOR, NOT"

The variable name in $(( )) can be replaced by adding a $ symbol in front of it, or not.

Copy code

[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

Copy code

Base conversion

$(( )) can convert other bases into decimal numbers and display them. The usage is as follows: echo $((N#xx))where N is the base and xx is a certain value in the base. After the command is executed, the value of the base number converted to decimal can be obtained.

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

(()) Redefine variable value

Copy code

[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

Copy code

Turn: the difference between shell $(( )), $( ), `` and ${}

The above is the Linux-related knowledge shared by Liangxu Tutorial Network for all friends.

Guess you like

Origin blog.csdn.net/manongxianfeng/article/details/113054828