且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一行上的Unix打印输出

更新时间:2023-12-04 12:04:46

您可以插入$(command)(新样式)或`command`(旧样式),以将命令的输出插入双引号字符串中./p>

You can insert $(command) (new style) or `command` (old style) to insert the output of a command into a double-quoted string.

echo "Welcome $(whoami)!"

注意:在脚本中,此方法可以正常工作.如果您在交互式命令行上尝试使用该命令,最终的!可能会给您带来麻烦,因为!会触发历史扩展.

Note: In a script this will work fine. If you try it at an interactive command line the final ! may cause you trouble as ! triggers history expansion.

命令替换

命令替换允许命令的输出替换命令名称.有两种形式:

Command Substitution

Command substitution allows the output of a command to replace the command name. There are two forms:

$(command)

`command`

Bash通过执行命令并将命令替换替换为命令的标准输出(,并删除所有尾随的换行符 [强调])来执行扩展.

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted [emphasis added].