如何在一行中回显“某些文本”和函数结果?

zer*_*_ik 0 bash scripts echo

我开始学习 Bash 脚本。我有一个问题。下面的代码工作正常,

Echo "Here is your current directory: "
pwd
Run Code Online (Sandbox Code Playgroud)

但是如果我想让 pwd 的结果与解释字符串写在同一行呢?怎么做?谢谢。

ste*_*ver 5

您可以使用命令替换

echo "Here is your current directory: $(pwd)"
Run Code Online (Sandbox Code Playgroud)

然而,你可能想进入更喜欢的习惯printfecho

printf 'Here is your current directory: %s\n' "$(pwd)"
Run Code Online (Sandbox Code Playgroud)

注意:你可以告诉echo不要包含终止的换行符,例如

echo -n "Here is your current directory: "
pwd
Run Code Online (Sandbox Code Playgroud)

但不推荐 - 请参阅为什么 printf 比 echo 更好?