0

There is one line in my bash script file like this:

echo "$string" | sed -e "s|\($str\)|$(wrap \\1 $1 $2)|"

Now, I want to wrap the $1 and $2 with double quotes for preventing errors while $1 equals to null.

I tried to add \ before ", like "\"$1\" \"$2\"" but it does not work.

How can I do that?

3
  • 4
    Using shell variables and command substitutions to generate a sed script is terribly fragile. Could you explain what $string and $str are and what you want to achieve. Are you generating commands that you later execute?
    – Kusalananda
    Commented Dec 12, 2018 at 10:24
  • 1
    and as always, when writing "but it does not work", please show the exact command(s) you've entered, the result, any error messages you get, and what you expected to get instead.
    – ilkkachu
    Commented Dec 12, 2018 at 10:46
  • Do you expect wrap to be invoked for every line with the captured string as argument or do you expect wrap to be given a literal \1 as argument and its output to be used to generate the content of the sed inline script? Commented Dec 12, 2018 at 13:19

1 Answer 1

0

I got the answer finally. I misunderstood the effect of double quotes. Actually, I only need to surround the characters that I need. So, I change the code like this:

echo "$string" | sed -e "s|\($str\)|"$(wrap \\1 "$1" "$2")"|g"
1
  • Yes. You are in a subshell inside the parentheses, so quotes in there are not affected by "outer" quotes. Commented Dec 12, 2018 at 16:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .