Assigning a value having semicolon (';') to a variable in bash
You don't need sed
(or any other regex engine) for this at all:
s='hello;'echo "${s//;/\;}"
This is a parameter expansion which replaces ;
with \;
.
That said -- why are you trying to do this? In most cases, you don't want escape characters (which are syntax) to be inside of scalar variables (which are data); they only matter if you're parsing your data as syntax (such as using eval), which is a bad idea for other reasons, and best avoided (or done programatically, as via printf %q
).
I find it interesting that the use of back-ticks gives one result (your result) and the use of $(...)
gives another result (the wanted result):
$ echo "hello;" | sed 's/\([^\\]\);/\1\\;/g'hello\;$ z1=$(echo "hello;" | sed 's/\([^\\]\);/\1\\;/g')$ z2=`echo "hello;" | sed 's/\([^\\]\);/\1\\;/g'`$ printf "%s\n" "$z1" "$z2"hello\;hello;$
If ever you needed an argument for using the modern x=$(...)
notation in preference to the older x=`...`
notation, this is probably it. The shell does an extra round of backslash interpretation with the back-ticks. I can demonstrate this with a little program I use when debugging shell scripts called al
(for 'argument list'); you can simulate it with printf "%s\n"
:
$ z2=`echo "hello;" | al sed 's/\([^\\]\);/\1\\;/g'`$ echo "$z2"seds/\([^\]\);/\1\;/g$ z1=$(echo "hello;" | al sed 's/\([^\\]\);/\1\\;/g')$ echo "$z1"seds/\([^\\]\);/\1\\;/g$ z1=$(echo "hello;" | printf "%s\n" sed 's/\([^\\]\);/\1\\;/g')$ echo "$z1"seds/\([^\\]\);/\1\\;/g$
As you can see, the script executed by sed
differs depending on whether you use x=$(...)
notation or x=`...`
notation.
s/\([^\]\);/\1\;/g # ``s/\([^\\]\);/\1\\;/g # $()
Summary
Use $(...)
; it is easier to understand.