Solution to the problem of insufficient permissions when using "sudo echo >>" in Linux

When using "sudo echo >>" in Linux, it prompts that the permissions are insufficient.
sudo echo "export PATH" >> /etc/profile
bash: /etc/profile: Permission denied

bash refuses saying insufficient permissions. This is because the redirection symbols ">" and ">>" are also bash commands. sudo only allows the echo command to have root permissions, but does not allow the ">" and ">>" commands to also have root permissions, so bash will think that these two commands do not have permission to write information.

Solution one:

Use the "sh -c" command, which allows bash to execute a string as a complete command, thus extending the scope of sudo's influence to the entire command . The specific usage is as follows:

sudo sh -c ‘echo "PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin/" >> /etc/profile’
sudo sh -c ‘echo "export PATH" >> /etc/profile’
Solution two:

Using pipes and the tee command, this command can read information from the standard input and write it to the standard output or file. The specific usage is as follows:

echo “PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin/” | sudo tee -a /etc/profile
echo “export PATH” | sudo tee -a /etc/profile

Note:
The tee command "-a" option has the same effect as the ">>" command. If this option is removed, the tee command has the same effect as the "> ;" Order.

Guess you like

Origin blog.csdn.net/change_can/article/details/115128218