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)
http://www.innovationsts.com/blog/?p=2758
As the instructions are not that clear above I am using the instructions from that blog post. With examples so it is easier to see what you need to do.
Notice that it’s the second command (the gzip command) in the pipeline that causes the error. That’s where our technique of using bash with the -c option comes in.
We can see form the ls command’s output that the compressed file creation succeeded.
The second method is similar to the first in that we’re passing a command string to bash, but we’re doing it in a pipeline via sudo.
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 thesudo
and not for it.You need to:
sudo)
sudo -s
(so thatsudo
uses a shell to process the quoted redirection.)STEP 1 create a function in a bash file (
write_pacman.sh
)'EOF'
will not interpret$arch
variable.STE2 source bash file
STEP 3 execute function
As @geekosaur explained, the shell does the redirection before running the command. When you type this:
Your current shell process makes a copy of itself that first tries to open
/some/file
for writing, then makes that file descriptor its standard output, and only then executessudo
.If you're allowed (sudoer configs often preclude running shells), you can do something like this:
But I find a good solution in general is to use
| sudo tee
instead of>
and| sudo tee -a
instead of>>
. That's especially useful if the redirection is the only reason I needsudo
in the first place; after all, needlessly running processes as root is precisely whatsudo
was created to avoid. And runningecho
as root is just silly.I added
> /dev/null
on the end becausetee
sends its output to both the named file and its own standard output, and I don't need to see it on my terminal. (Thetee
command acts like a "T" connector in a physical pipeline, which is where it gets its name.) And I switched to single quotes ('
...'
) instead of doubles ("
..."
) so that everything is literal and I didn't have to put a backslash in front of the$
in$arch
.So that takes care of writing to files as root using
sudo
. Now for a lengthy digression on ways to output newline-containing text in a shell script. :)First, you can just group all of the
echo
's together in a subshell, so you only have to do the redirection once:Or use
printf
instead ofecho
, so you can embed newlines directly into the string using\n
. And need to do so at the end of the string, sinceprintf
, unlikeecho
, doesn't automatically append a newline:In
bash
, you can get the same result withecho -e
:But most shells will just output the
-e
when you try that, so it's not recommended.With both
printf
andecho -e
, what the command gets as an argument string contains a literal backslash followed by a literal N wherever you type\n
, and it's up to the command program itself (the code insideprintf
orecho
) to translate that into a newline. In many modern shells, you have the option of using ANSI quotes$'
...'
, which will translate sequences like\n
into literal newlines before the command program ever sees the string, which means such strings work with any command whatsoever:But, while more portable than
echo -e
, ANSI quotes are still a non-POSIX extension.My preferred way of doing this would be to use a here-document and avoid the need for
echo
orprintf
entirely: