Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo line with multiple quotes/special characters into file?

I am trying to echo the following line into a .profile but it keeps getting confused by either the many quotes or special characters.

bind '"e[A": history-search-backward'

I've tried all sorts of things but can't get it nailed.

This is what I currently have:

sudo su -c 'echo "bind \'\"\\e[A\": history-search-backward\'" >> /etc/profile' -

This is what it returns:

su: user '"\e[A": does not exist

Yet if I just use:

echo bind \'\"\\e[A\": history-search-backward\'" >> /home/user/testfile

It works just fine.

I have all manner of "sudo su -c "echo blah..." in the rest of my script that work just fine.

Any ideas?

like image 232
user1604139 Avatar asked Aug 16 '12 20:08

user1604139


1 Answers

Try this

sudo su -c $'echo \"bind \'\"\\e[A\": history-search-backward\'\" >> /etc/profile\' -'

From the bash man page:

A single quote may not occur between single quotes, even when preceded by a backslash.

Text quoted by $'...' may contain backslash-escaped quotes, both single and double.

Another option is to add a simpler expression to ~/.inputrc:

echo '"\e[A": history-search-backward' >> ~/.inputrc

There doesn't seem to be a system-wide equivalent of .inputrc that is read by all users. Also, this makes the key binding available to any program that uses readline. If you really do want to restrict it to bash, add a conditional expression:

cat >> ~/.inputrc <<'EOF'
$if Bash
"\e[A": history-search-backward
$endif
EOF
like image 115
chepner Avatar answered Sep 29 '22 02:09

chepner