我正在创建一个bash脚本来设置一个ubuntu 16.04lts操作系统来下载、安装和其他东西,而不需要分别介绍每个命令,我必须在/etc/profile文件中编写来添加一个path环境变量。当我的代码进入该行时,显示“权限被拒绝”消息,这就是我所拥有的:
sudo echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile bash: /etc/profile: Permission denied
你知道我怎么解决这个问题吗?
发生Shell I/O重定向 之前 shell执行您的命令…换句话说,当您编写:
sudo somecommand >> /etc/profile
这个 >> /etc/profile 部分作为当前用户执行,而不是作为 root 是的。这就是你收到“拒绝许可”信息的原因。解决这个问题的方法有很多种。您可以运行:
>> /etc/profile
root
sudo sh -c "echo export PATH=$PATH:/usr/local/go/bin >> /etc/profile"
或者你可以利用附加的( -a )标记到 tee 命令:
-a
tee
echo "export PATH=$PATH:/usr/local/go/bin" | sudo tee -a /etc/profile