0

I've experiencing problems using variables as parameters for sed.

My sample:

set -x
cat sample.txt
variable=123
parameter='s/ab/'${variable}'/g'
cat sample.txt | sed $parameter
variable=123
parameter='s/a b/'${variable}'/g'
cat sample.txt | sed $parameter
cat sample.txt | sed 's/a b/123/g'
parameter="'s/ab/'${variable}'/g'"
cat sample.txt | sed $parameter

My result:

+ cat sample.txt
a b
ab
+ variable=123
+ parameter=s/ab/123/g
+ cat sample.txt
+ sed s/ab/123/g
a b
123
+ variable=123
+ parameter='s/a b/123/g'
+ cat sample.txt
+ sed s/a b/123/g
sed: 1: "s/a": unterminated substitute pattern
+ cat sample.txt
+ sed 's/a b/123/g'
123
ab
+ parameter=''\''s/ab/'\''123'\''/g'\'''
+ cat sample.txt
+ sed ''\''s/ab/'\''123'\''/g'\'''
sed: 1: "'s/ab/'123'/g'": invalid command code '

As you can see, when there is a space into the pattern to search my solution doesn't work and I don't know why. In the last try even weirder things starts to happen...

I've been trying and this works:

set -x
cat sample.txt
variable=123
parameter="s/a b/"${variable}"/g"
echo $parameter
cat sample.txt | sed "$parameter"
+ cat sample.txt
a b
ab
+ variable=123
+ parameter='s/a b/123/g'
+ echo s/a b/123/g
s/a b/123/g
+ cat sample.txt
+ sed 's/a b/123/g'
123
ab

It seems that sed or bash ¿? puts the single quote for me...

Can anyone explain this behaviour?

minyatur
  • 21
  • 1
  • 5
  • 1
    Duplicate of [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – oguz ismail Apr 10 '20 at 10:06
  • @oguzismail - the question here is not "when" to use quotes, but "how" to use them. This is not the same question as the one you linked to. – mathguy Apr 10 '20 at 14:03
  • @mathguy I know. Still it's better to close these questions as duplicate since they all boil down to quotation issues. – oguz ismail Apr 10 '20 at 14:25
  • @oguzismail - As far as I can tell, the issue in this case is not the proper use of quotes, but the fact that `sed`, called the same way as the OP shows, expands to `sed -e ' ... '` with the single-quotes added **automatically** for some reason. I don't see **anything** about that in the question you linked to. Do you? If you don't, then this answer **does** add something - and in fact the thread you linked to has nothing useful for the OP. – mathguy Apr 10 '20 at 14:30
  • All OP had to do was quoting `$parameter` in his first try, answers to the linked Q demonstrate that clearly. Single quotes are not added to the parameter *automatically*, that's the way xtrace shows strings containing quotes. – oguz ismail Apr 10 '20 at 14:37
  • 1
    bash doesn't treat quotes in variables' values the way you expect; see [this question](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quotes-in-arguments-passed-to-it-through-variables). Quotes go *around* data, not *in* data. – Gordon Davisson Apr 10 '20 at 16:06
  • 1
    I agree with @GordonDavisson, and I think [the answer to that question](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quotes-in-arguments-passed-to-it-through-variables) actually answers this too. – Enlico Apr 10 '20 at 18:15

0 Answers0