- Learning Linux Shell Scripting
- Ganesh Naik
- 245字
- 2021-06-25 22:02:53
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.
推薦閱讀
- Hands-On Intelligent Agents with OpenAI Gym
- 現(xiàn)代測控電子技術(shù)
- Project 2007項(xiàng)目管理實(shí)用詳解
- 程序設(shè)計(jì)缺陷分析與實(shí)踐
- 一本書玩轉(zhuǎn)數(shù)據(jù)分析(雙色圖解版)
- Learning Apache Cassandra(Second Edition)
- 分布式多媒體計(jì)算機(jī)系統(tǒng)
- VB語言程序設(shè)計(jì)
- 嵌入式操作系統(tǒng)
- Ruby on Rails敏捷開發(fā)最佳實(shí)踐
- Mastering ServiceNow Scripting
- 嵌入式操作系統(tǒng)原理及應(yīng)用
- Building a BeagleBone Black Super Cluster
- 大數(shù)據(jù)案例精析
- Artificial Intelligence By Example