3shell command substitution

Shell is a command substitution refers to the output of the command is assigned to a variable . Save content, for example, will use the ls command to view a directory to a variable, which requires the use command substitution.

Shell There are two ways to complete command substitution, one is anti-quotation marks ` `, one is$() used as follows:

variable=`commands`
variable=$(commands)

Where, variable is a variable name, commands is the command to be executed. commands may be only one command, there may be a plurality of commands to command a plurality semicolon ;separated.

script
result
Knowledge Point
  1. #!/bin/bash
  2. BEGIN_TIME = `date`  # start time, using the` `replacement
  3. 20S SLEEP  # sleep 20 seconds
  4. finish_time = $ (DATE)  # end time, using the $ () to replace
  5. echo "Begin time: $begin_time"
  6. echo "Finish time: $finish_time"
Run the script, after 20 seconds you can see the output:
the Begin Time: 2019 Nian 04 CST Friday, 19 October 09:59:58
Finish Time: 2019 Nian 04 Friday, 19 October 10:00:18 CST
date command to get the current system time
  1. #!/bin/bash
  2. BEGIN_TIME = 'DATE  +% s`  # start time, `` replacement
  3. 20S SLEEP  # sleep 20 seconds
  4. finish_time = $ (DATE  +% S # end time, using the $ () Replace
  5. run_time = $ ( (finish_time - BEGIN_TIME ))  # time difference
  6. echo "begin time: $begin_time"
  7. echo "finish time: $finish_time"
  8. echo "run time: ${run_time}s"
Run the script, after 20 seconds you can see the output:
the begin Time: 1,555,639,864
Finish Time: 1,555,639,884
RUN Time: 20S

1. date command %sformat control character can get the current UNIX timestamp,

UNIX timestamp refers to the number of seconds from January 1, 1970 00:00:00 so far.

Note that there is a space, between the date and date + time 2.% s,% s + and no space between

3. Line 5 code is Shell command mathematical calculations , and C ++, C #, Java and other programming languages,(( ))

Data calculated in the Shell is not so easy, you must use a special mathematical calculations command

  1. #!/bin/bash
  2. LS = $ (ls -l)
  3. echo 'without the double quotes' $ LS
  4. echo '------------------------'
  5. echo 'double quotes' "$ LS"

In order to prevent confusion format, we recommend double quotes if the output variable.

The total amount of double quotation marks without 4 -rw-rw-r-- 1 roaddb roaddb 14. 8 dated 26 04:29 aaa.txt -rw-rw-r-- 1 roaddb roaddb 26. 8 dated 20 09:05 a.sh

------------------------

The total amount of double quotes 4

-rw-rw-r-- 1 roaddb roaddb   14 8月  26 04:29 aaa.txt

-rw-rw-r-- 1 roaddb roaddb   26 8月  20 09:05 a.sh

If the output is replaced by the contents of the command include a plurality of rows (i.e., a line break ), or comprising a plurality of contiguous blank character ,

Then should the variable with double quotes if the output variable , otherwise the system will use the default white space to fill,

This causes the invalid wrap, and is continuously compressed into a whitespace

Shell math

Talk about anti-quotation marks and $ ()

原则上讲,上面提到的两种变量替换的形式是等价的,可以随意使用;但是,反引号毕竟看起来像单引号,有时候会对查看代码造成困扰,而使用 $() 就相对清晰,能有效避免这种混乱。而且有些情况必须使用 $(),$() 支持嵌套,反引号不行。

$()支持嵌套,举栗

Fir_File_Lines=$(wc -l $(ls | sed -n '1p'))
echo "$Fir_File_Lines"
结果是:36 anaconda-ks.cfg

要注意的是,$() 仅在 Bash Shell 中有效,而反引号可在多种 Shell 中使用

 

Guess you like

Origin www.cnblogs.com/mianbaoshu/p/12069458.html