如何在 BASH 中打印(回显)消息而不换行?
我正在创建一个 .bash_profile 脚本,但遇到了一个小问题。
这是我的代码片段:
echo -n "Welcome "
whoami
echo -n "!"
我希望输出给出类似这样的内容:
Welcome jsmith!
...而不是这样:
Welcome jsmith
!
如何将所有这些都放到一行上?
非常感谢任何帮助。如果有帮助的话,我正在 Ubuntu Server 10.04 LTS 上使用 Bash Shell。
I am creating a .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 of this:
Welcome jsmith
!
How can I get all of this onto one line?
Any help is greatly appreciated. If it helps, I'm using the Bash Shell, on Ubuntu Server 10.04 LTS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以插入
$(command)
(新样式)或`command`
(旧样式)以将命令的输出插入双引号字符串中。注意:在脚本中这可以正常工作。如果您在交互式命令行中尝试,最后的
!
可能会给您带来麻烦,因为!
会触发 历史扩展。You can insert
$(command)
(new style) or`command`
(old style) to insert the output of a command into a double-quoted string.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.使用此表格。摆脱
echo
并避免创建子shell。Use this form. Get rid of
echo
and get away from creating a subshell.试试这个:
或者
Try this:
OR
您可能想要:
$()
构造执行其中的命令,并计算其输出。另一种选择是:
虽然这有点疯狂。
无论您做什么,您可能都需要在
!
两边加上单引号。在我的 shell 中,!
是一个历史元字符,即使在双引号内也是如此。You probably want:
The
$()
construct executes the command inside it, and evaluates to the output of it.Another option would be:
Although that's a bit mad.
Whatever you do, you might need single quotes around the
!
. In my shell,!
is a history metacharacter even inside double quotes.你可以这样做:
You can do something like: