# Using sudo with a redirect



## dragonbite (May 9, 2013)

I am trying to add a line to the /etc/rc.conf file via a script (so I can recreate it automatically) which requires using sudo (I do not have the root's password).  I get a permissions error when I try `sudo echo 'apache22_enable="YES"' >> /etc/rc.conf` and I believe that sudo is handling the `echo 'apache22_enable="YES"'` but is not handling the `>> /etc/rc.conf` portion.

How do I get sudo to apply the echo to the file?

Here are a couple of things I have tried:
	
	



```
$ sudo echo 'apache22_enable="YES"' >> /etc/rc.conf
cannot create /etc/rc.conf: Permission denied
$
$ sudo -c "echo 'apache22_enable="YES"' >> /etc/rc.conf"
sudo: unknown login class: echo 'apache22_enable=YES' >> /etc/rc.conf
sudo: unable to initialize policy plugin
$
$ sudo -s "echo 'apache22_enable="YES"' >> /etc/rc.conf"
#
# exit
$
```

Since I don't have the root password I cannot run `su -C "[i]some command here[/i]"`


----------



## phoenix (May 9, 2013)

```
# sudo su -
# echo 'apache22_enable="YES"' >> /etc/rc.conf
```
Even safer:

```
# sudoedit /etc/rc.conf
```
Then you can just copy/paste or even type the info in.


----------



## derekschrock (May 9, 2013)

`sudo sh -c "echo 'apache22_enable="YES"' >> /etc/rc.conf"`


----------



## dragonbite (May 10, 2013)

I ran this command





			
				phoenix said:
			
		

> ```
> # sudo su -
> # echo 'apache22_enable="YES"' >> /etc/rc.conf
> ```



And I got this entered into the file. 
	
	



```
apache22_enable="YES"
```
 It matches the other flags already present in the rc.conf file.

Then I tried this command





			
				derekschrock said:
			
		

> `sudo sh -c "echo 'apache22_enable="YES"' >> /etc/rc.conf"`


 It worked, but the " and ' must have confused things a bit because what was entered into the file did not include the " around "YES". 
	
	



```
apache22_enable=YES
```

I do prefer to second way, as it doesn't drop me into full-blown-root, but I don't know what the effect of the missing "" could produce.


----------



## junovitch@ (May 15, 2013)

Escape the quotes in the middle like this.

`sudo sh -c "echo 'apache22_enable=\"YES\"' >> /etc/rc.conf"`


----------

