1

For ubuntu 12.04 bash scripts, I used to be able to run commands like the example below to overwrite files:

sudo echo 'line1   
line2
line3' > /etc/some/config/file.conf

Ever since Ubuntu 14.04, I now have to break it up to get the same behaviour like so:

sudo echo 'line1
line2
line3' | sudo tee /etc/some/config/file.conf

I am guessing this is an extra security measure? Is there some way to go back to the old behaviour?


UPDATE

As some have correctly pointed out, this behaviour is consistent across Ubuntu 12.04 and Ubuntu 14.04. I believe I got mixed but because if one runs the command in a bash script called with sudo, then it executes fine, however, pasting the command into the CLI, it results in permission denied. I was comparing apples with oranges.

Programster
  • 5,871

1 Answers1

6

You cannot directly use > with sudo the way you are doing it, the reason is the redirection is performed by the shell and not by sudo and hence the command will fail to write to the file because of permissions.

Refer to this question for appropriate solutions for this:

Update:

In case you copy:

sudo echo 'line1   
line2
line3' > /etc/some/config/file.conf

to a file, say scrip.sh, and then execute the script as

sudo bash script.sh

you won't get an error and the script will execute successfully because in this case, you are running the script by providing the shell with appropriate permissions(sudo bash).

jobin
  • 27,708