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

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.

主站蜘蛛池模板: 乡宁县| 武川县| 平昌县| 威宁| 威宁| 若尔盖县| 临澧县| 略阳县| 柞水县| 佳木斯市| 东安县| 常熟市| 尼勒克县| 武汉市| 罗源县| 武安市| 商城县| 临沭县| 冀州市| 桐柏县| 广安市| 河曲县| 沙洋县| 石棉县| 龙州县| 平安县| 井陉县| 武邑县| 西丰县| 东平县| 黄石市| 东至县| 全南县| 大方县| 澄城县| 泸西县| 沅江市| 聊城市| 金坛市| 贵德县| 长垣县|