Paul Sender
Paul Sender

Reputation: 488

How can I echo a shell command as a string

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?

Upvotes: 0

Views: 2163

Answers (2)

dan
dan

Reputation: 5231

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 (').

Upvotes: 1

&#212;rel
&#212;rel

Reputation: 7622

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

Will add

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

at the end of your .zshrc file

Upvotes: 3

Related Questions