我开始学习 Bash 脚本。我有一个问题。下面的代码工作正常,
Echo "Here is your current directory: "
pwd
Run Code Online (Sandbox Code Playgroud)
但是如果我想让 pwd 的结果与解释字符串写在同一行呢?怎么做?谢谢。
您可以使用命令替换
echo "Here is your current directory: $(pwd)"
Run Code Online (Sandbox Code Playgroud)
然而,你可能想进入更喜欢的习惯printf了echo
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 更好?