如何将这两个示例与 pushd 和 whoami 结合使用来更改目录?
我知道我可以像这样更改目录:
#!/bin/bash
pushd /home/mike/Pictures > /dev/null
# do something in the new dir
ls
popd > /dev/null
我知道我可以得到这样的用户名:
#!/bin/bash
theuser=`whoami`
echo $theuser
你想多了……
cd ~/Pictures
编辑:
实际上,没有。你真正想要的是:
cd "$(xdg-user-dir PICTURES)"
这些反引号可用于将它们包含的命令的输出插入到另一个中:
pushd /home/`whoami`/Pictures
仅用于cd
更改目录更容易:
#!/bin/bash
cd ~/Pictures
比使用pushd
and容易得多popd
,在子 shell 中运行命令:
(
cd /home/$(whoami)/Pictures &&
ls
)
子外壳更改目录,而不影响主进程 - 完全按照您的意愿,但更可靠。
Bash 已经有了 $USER 变量,不需要调用外部二进制文件
pushd /home/$USER/Pictures > /dev/null
在其他解决方案中:
pushd "$HOME/Pictures"
毕竟,没有什么要求主目录带有用户名!