Home > Back-end >  How can I echo a shell command as a string
How can I echo a shell command as a string

Time:07-01

I want place the following string into my .zshrc file using the command line

eval "$(docker exec -it <abc-123>)"

I've tried:

echo "eval "$(docker exec -it <abc-123>)"" >> .zshrc

and every other ` and ' combination

The result I want is to have my .zshrc file execute eval "$(docker exec -it <abc-123>)" much like it does for homebrew eval "$(/opt/homebrew/bin/brew shellenv)"

I just want to be able to write to my .zshrc file using echo. How can I achieve this?

CodePudding user response:

echo 'eval "$(docker exec -it <abc-123>)"' >> .zshrc

Will add

eval "$(docker exec -it <abc-123>)"

at the end of your .zshrc file

CodePudding user response:

A here doc can print a string verbatim, without quoting issues:

cat <<"EOF" >> .zshrc
eval "$(docker exec -it <abc-123>)"
EOF

In this case, you could do echo 'eval "$(docker exec -it <abc-123>)"' >> .zshrc, provided <abc-123> doesn't contain a single quote (').

  • Related