官术网_书友最值得收藏!

Command substitution

On a keyboard, there is one interesting key, the backward quote, `. This key is normally situated below the Esc key. If we place text between two successive backquotes, then echo will execute those as commands instead of processing them as plain text.

Alternate syntax for $(command) is the backtick character `, which we can see as follows:

    $(command) or `command`

For example:

  • We need to use proper double quotes, as follows:
    $ echo "Hello, whoami"  
  • The next command will print the text as it is; such as Hello, whoami:
    Hello, whoami
  • Use proper double quotes and single backquotes:
    $ echo "Hello, `whoami`."
    Hello, student
  • When we enclose whoami text in the ` characters, the same text that was printed as plain text will run as a command, and command output will be printed on the screen.
  • Use proper double quotes:
    $ echo "Hello, $(whoami)."
    Hello, student.
  • This is the same as earlier.

Another example:

    echo "Today is date"

Output:

    Today is date  

A similar example:

    /

Another example is:

    echo "Today is $(date)"  

The output is:

    Today is Fri Mar 20 15:55:58 IST 2015  

Furthermore, similar examples include:

    $ echo $(cal)

In this example, new lines are lost.

Another example includes:

    $ echo "$(cal)"  

Here, the display is properly formatted.

Next, the nesting of commands is as follows:

    $ pwd
    /home/student/work
    $ dirname="$(basename $(pwd)) "
    $ echo $dirname

This command shows us that the base directory for the current directory is student.

主站蜘蛛池模板: 分宜县| 乌拉特前旗| 丽江市| 措美县| 体育| 连山| 长顺县| 松滋市| 郎溪县| 固原市| 桑植县| 广宗县| 苍溪县| 民乐县| 上饶县| 惠水县| 朝阳县| 仁布县| 临海市| 年辖:市辖区| 东辽县| 内乡县| 安宁市| 双峰县| 冕宁县| 通渭县| 黑河市| 沁水县| 涿鹿县| 黔西县| 临清市| 颍上县| 沙河市| 自治县| 闻喜县| 泰兴市| 桐城市| 基隆市| 黑水县| 博白县| 枣强县|