如何在bash脚本中使用变量
user= whoami
echo $user
cd /home/${user}
我无法通过此方法更改目录。
输出
pbsh56@pbsh56:~/Documents$ source one.sh
pbsh56
pbsh56@pbsh56:/home$
user= whoami
echo $user
cd /home/${user}
I unable to change directory by this method.
output
pbsh56@pbsh56:~/Documents$ source one.sh
pbsh56
pbsh56@pbsh56:/home$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
间距很重要。由于命令中存在空格,这不会执行您期望的操作:
它的作用是定义一个名为
user
的空变量,然后使用该变量调用whoami
命令添加到环境中的空变量。您看到的“pbsh56”来自whoami
命令,而不是来自echo
。你想要的是
Spacing is important. Because of the space in your command, this doesn't do what you expect:
What that does is define an empty variable called
user
, and then calls thewhoami
command with that empty variable added to the environment. The "pbsh56" you see is from thewhoami
command, not from theecho
.What you want is