一行上的Unix打印输出 [英] Unix Print Output on One Line
问题描述
我正在创建Unix .bash_profile脚本,但遇到了一个小问题.这是我的代码段:
I am creating a Unix .bash_profile script, and I have run into a small problem. Here is a snippet of my code:
echo -n "Welcome "
whoami
echo -n "!"
我希望输出给出这样的内容:
I would like the output to give something like this:
Welcome jsmith!
...相反,我得到的是这样的东西:
... instead, I am getting something like this:
Welcome jsmith
!
如何将所有这些整合到一行?任何帮助是极大的赞赏.如果有帮助,我将在Ubuntu Server 10.04 LTS上使用Bash Shell.
How can I get all of this onto one line? Any help is greatly appreciated. If this helps, I am using the Bash Shell, on Ubuntu Server 10.04 LTS.
推荐答案
您可以插入$(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].
这篇关于一行上的Unix打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!