I am generating sed expression in Bash script, and I am having a following problem.

I want to add a space in front of each appearance of abc.

This commands works as expected

echo abc | sed -e "s/abc/ def/"

output is " def" without quotes

whereas this commands

Q='-e "s/abc/ def/"'
echo abc | sed $Q

outputs sed: -e expression #1, char 1: unknown command: `"'

Outside of bash script, everything works.

I tried all possible ways of quotes, escaping characters, but couldn't figure it out. Can someone help me?


Use an array of options, not a single one:

Q=( -e "s/abc/ def/" )
echo abc | sed "${Q[@]}"

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.