I am trying to automate the addition of a repository source in my arch's pacman.conf file but using the echo
command in my shell script. However, it fails like this:-
sudo echo "[archlinuxfr]" >> /etc/pacman.conf
sudo echo "Server = http://repo.archlinux.fr/\$arch" >> /etc/pacman.conf
sudo echo " " >> /etc/pacman.conf
-bash: /etc/pacman.conf: Permission denied
If I make changes to /etc/pacman.conf manually using vim, by doing
sudo vim /etc/pacman.conf
and quiting vim with :wq
, everything works fine and my pacman.conf has been manually updated without "Permission denied" complaints.
Why is this so? And how do I get sudo echo
to work? (btw, I tried using sudo cat
too but that failed with Permission denied as well)
The problem is that the redirection is being processed by your original shell, not by sudo
. Shells are not capable of reading minds and do not know that that particular >>
is meant for the sudo
and not for it.
You need to:
sudo)
sudo -s
(so that sudo
uses a shell to process the quoted redirection.)