5

As an example: When installing pyenv, you're required to add

export PATH="/home/user/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

to your .bashrc file.

However, the usual echo '..' >> ~/.bashrc will evaluate the statements before appending. How do you properly append such statements to a file using the command line without evaluation?

  • 4
    This will not evaluate the commands, it will only print them into your ~/.bashrc. Either provide more information about your system and setup, or open ~/.bashrc in a text editor and write the lines by hand – j-money May 7 at 17:33
  • @j-money This was merely meant as an example. I have noticed that there may be a discrepancy between the results of using echo '..' vs echo .. and as such, simply piping line by line into the file may indeed solve the issue. Nevertheless, steeldriver has provided a concise and scalable solution. – bhnn May 7 at 23:13
16

I'd suggest a here document

$ cat >> .bashrc <<'EOF'
export PATH="/home/user/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
EOF

So long as the EOF word (which can be anything) is quoted, no shell expansion of the body takes place.

5

However, the usual echo '..' >> ~/.bashrc will evaluate the statements before appending. How do you properly append such statements to a file using the command line without evaluation?

No it won't. Anything inside of single-quotes is completely un-evaluated. You can use
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc without any worry that anything will be interpreted. The only character of any significance in a single-quoted string is a single-quote (which ends the string, and cannot be escaped.)

2

You can add \ in front of special characters.

For example:

echo export PATH=\"/home/user/.pyenv/bin:\$PATH\"

gives the following result:

export PATH="/home/user/.pyenv/bin:$PATH"

so the quotation marks and the $PATH are still there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.