Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write in /etc/profile using bash | Permission Denied

I'm creating a bash script to set up an Ubuntu 16.04 lts OS to download, install and other stuff without introduce each command separately and I have to write in the /etc/profile file to add a PATH environment variable. When my code get into that line it appears the Permission Denied message, this is what I have:

sudo echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
bash: /etc/profile: Permission denied

Do you know how could I solve this?

like image 634
Javier Salas Avatar asked Jun 25 '18 19:06

Javier Salas


People also ask

How do I fix bash Permission denied in Linux?

The Bash permission denied error indicates you are trying to execute a file which you do not have permission to run. To fix this issue, use the chmod u+x command to give yourself permissions. If you cannot use this command, you may need to contact your system administrator to get access to a file.

How do I run shell script Permission denied?

To fix the permission denied error in Linux, one needs to change the file permission of the script. Use the “chmod” (change mode) command for this purpose.

Why is permission denied Linux?

While using Linux, you may encounter the error, “permission denied”. This error occurs when the user does not have the privileges to make edits to a file. Root has access to all files and folders and can make any edits. Other users, however, may not be allowed to make such edits.


Video Answer


1 Answers

Shell i/o redirection happens before the shell executes your command...in other words, when you write:

sudo somecommand >> /etc/profile

The >> /etc/profile part is performed as the current user, not as root. That's why you're getting the "permission denied" message. There are various ways of solving this. You can run:

sudo sh -c "echo export PATH=$PATH:/usr/local/go/bin >> /etc/profile"

Or you can take advantage of the append (-a) flag to the tee command:

echo "export PATH=$PATH:/usr/local/go/bin" | sudo tee -a /etc/profile
like image 76
larsks Avatar answered Oct 04 '22 07:10

larsks