Write to a Shell Script file

Asked

Viewed 252 times

0

I have a problem writing a line in a file using Shell Script. I’ve tried using sed and echo, but in both I don’t have the expected behavior.

Using the sed:

sed -i -e 'export LS_OPTIONS='--color=auto'' /root/.bashrc

Using the echo:

echo "eval "`dircolors`"" >> /root/.bashrc

What I believe may be happening is that sed uses the simple quotes (') to start and finish what should be written, but since what I have to write has simple quotes, it cannot understand which should be the beginning and the end.

And using echo, as it uses double quotation marks (") instead of the simple ones like sed, it also does not know where is the beginning and end of what I intend to write.

I tried to declare a variable in the script and use echo to write to the file, but it also didn’t work, I did it as follows:

variavel=alias ll='ls $LS_OPTIONS -l'

And then where I wish to write, as follows:

echo $variavel >> /root/.bashrc

Does anyone know what I might be doing to write these lines in that file? Or some other command to use instead of echo or sed. From now on I thank you all for your help.

I apologize if I got disorganized or some idea misexpressed. It is my first question that put here in the stack.

  • 1

    "Escape" quotes with backslash \ or use " ' ", or, ' " '

  • Valdeir, could you explain to me how I perform the use of the backslash instead of the quotation marks? Assuming I was going to use echo, I’d be like this echo \eval "dircolors"\ >> /root/.bashrc ?

  • Valderir meant that you have to put the in front of the quotes you want to add in the file. Putting the backslash you say to the system shows the quotes. <br> Ex.:<br><br> <code>echo "Eval "\dircolors\\"" >> /root/. bashrc</code><br><br> Output<br> <code>Eval 'dircolors'</code>

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.