$ sudo echo "echo hello" > /usr/local/bin/hello
提示没有权限。
sudo bash -c 'echo "echo hello" > /usr/local/bin/hello'
tee
而不是 STDOUT
重定向写入文件:sudo echo "echo helo" | sudo tee /usr/local/bin/hello >/dev/null
在自动化 Shell 脚本中,可以使用该方式:
echo $USER_PW | sudo -S bash -c 'cat >> /etc/security/limits.conf << EOF
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
EOF'
如何在Linux上重定向sudo命令的STDOUT并以root身份写入文件?_cuma2369的博客-CSDN博客
→ How to redirect STDOUT of sudo command on Linux and write to file as root? - SysTutorials