Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to do this:

echo alias get_python_lib='python -c "from distutils.sysconfig import get_python_lib; print get_python_lib();"' >> ~/.bash_aliases

The result in my ~/.bash_aliases file is:

alias get_python_lib=python -c "from distutils.sysconfig import get_python_lib; print get_python_lib();"

Unfortunately, the echo command stripped the first set of apostrophes ('), and the alias does not work.

I've tried all manner of apostrophe and quote swaps and used all the commands I could find as a substitute (echo, type, cat, printf), but nothing has worked.

How do I append a line of text to a file when the line of text requires all of its apostrophes and quotes?

share|improve this question
up vote 1 down vote accepted

Escaping the quotes works and adding wrapper quotes works:

echo alias get_python_lib=\'python -c "\"from distutils.sysconfig import get_python_lib; print get_python_lib();\""\' >> ~/.bash_aliases

$ cat .bash_aliases
alias get_python_lib='python -c "from distutils.sysconfig import get_python_lib; print get_python_lib();"'
share|improve this answer

Or stick double quotes around all the arguments to echo and escape the inner double quotes

echo "alias get_python_lib='python -c \"from distutils.sysconfig import get_python_lib; print get_python_lib();\"'" >> ~/.bash_aliases
share|improve this answer
    
Thanks for this answer; I used your solution just because it's a little simpler. Unfortunately I could only mark one as the "correct" answer, and the other was first. – Joseph Oct 12 '11 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .