Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have some VM stuff set up in my mamp instance, I'm using wp-cli in a bash to install wordpress instances, I wanted to add to this script a command to edit the hosts file based on a var.

sudo echo -e "127.0.0.1\timtest\n" >> /etc/hosts

-bash: /etc/hosts: Permission denied
share|improve this question
    
See my answer to "How do I use sudo to redirect output to a location I don't have permission to write to?" from Stack Overflow. –  Cristian Ciupitu Jun 9 at 17:22

1 Answer 1

up vote 2 down vote accepted

I think the >> gets associated with the sudo command, not with the echo command you're trying to run. You run echo with root perms, but the shell associates the >> with the sudo, no special perms.

I'd try it more like:

sudo bash -c 'echo -e "127.0.0.1\timtest\n" >> /etc/hosts'

(with a test run as sudo bash -c 'echo -e "127.0.0.1\timtest\n" >> /etc/hosts.new' for testing)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.