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 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

2 Answers 2

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. –  johsephm Oct 12 '11 at 12:41

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.