I cannot not use a shell variable in
sed in the
$NUMBER form. I have this line in my shell script:
cat shared.txt sed 's/whatever1/$2 ... whatever2/' > shared2.txt
The result in
shared2.txt looks like:
What did I do wrong?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
While using double quotes works, there are certain circumstances where this won't give what you want. For example,
t="bcd" echo '123$tbcd' | sed "s/$t$t//"
(yes, this is somewhat contrived!). You can avoid this by either escaping certain characters:
echo '123$tbcd' | sed "s/\$t$t//"
but it is easy to miss this. The safest way, in my opinion, is to surround the variables with double quotes (so that spaces don't brake the sed command) and surround the rest of the string with single quotes (to avoid the necessity of escaping certain characters):
echo '123$tbcd' | sed 's/$t'"$t"'//'.