How to append code verbatim to .bashrc?
Solution 1:
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.
Solution 2:
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 useecho '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.)
Solution 3:
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.