Skip to main content
Tweeted twitter.com/StackUnix/status/1166953747760726018
Became Hot Network Question
fixed inconsistency
Source Link
Bastian
  • 161
  • 1
  • 3

I have a variable, e.g. a'b, containing a single quote which I need to replace by two single quotes before writing it to a file: a''b.

The following bash code used to get the job done for me ...

line="a'b"
echo "${line//\'/\'\'}" > out.txt

... until today when I discovered diverging outputs depending on the version of bash being used:

  • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu): a''b
  • GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu): a\'\'b

I tried to modify the line above in numerous ways but was unable to have it produce the same output a''b in both bash shells.

I ended up using a combination of printfecho and sed instead,

echo "$(echo $line | sed "s/'/''/g")" > out.txt

but I am still wondering if the job can be gotten done with a more concise pure bash expression. Can it?

I have a variable, e.g. a'b, containing a single quote which I need to replace by two single quotes before writing it to a file: a''b.

The following bash code used to get the job done for me ...

line="a'b"
echo "${line//\'/\'\'}" > out.txt

... until today when I discovered diverging outputs depending on the version of bash being used:

  • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu): a''b
  • GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu): a\'\'b

I tried to modify the line above in numerous ways but was unable to have it produce the same output a''b in both bash shells.

I ended up using a combination of printf and sed instead,

echo "$(echo $line | sed "s/'/''/g")" > out.txt

but I am still wondering if the job can be gotten done with a more concise pure bash expression. Can it?

I have a variable, e.g. a'b, containing a single quote which I need to replace by two single quotes before writing it to a file: a''b.

The following bash code used to get the job done for me ...

line="a'b"
echo "${line//\'/\'\'}" > out.txt

... until today when I discovered diverging outputs depending on the version of bash being used:

  • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu): a''b
  • GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu): a\'\'b

I tried to modify the line above in numerous ways but was unable to have it produce the same output a''b in both bash shells.

I ended up using a combination of echo and sed instead,

echo "$(echo $line | sed "s/'/''/g")" > out.txt

but I am still wondering if the job can be gotten done with a more concise pure bash expression. Can it?

replaced printf by echo to reflect my current workflow. This was also suggested by commenters
Source Link
Bastian
  • 161
  • 1
  • 3

I have a variable, e.g. a'b, containing a single quote which I need to replace by two single quotes before writing it to a file: a''b.

The following bash code used to get the job done for me ...

line="a'b"
echo "${line//\'/\'\'}" > out.txt

... until today when I discovered diverging outputs depending on the version of bash being used:

  • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu): a''b
  • GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu): a\'\'b

I tried to modify the line above in numerous ways but was unable to have it produce the same output a''b in both bash shells.

I ended up using a combination of printf and sed instead,

echo "$(printfecho $line | sed "s/'/''/g")" > out.txt

but I am still wondering if the job can be gotten done with a more concise pure bash expression. Can it?

I have a variable, e.g. a'b, containing a single quote which I need to replace by two single quotes before writing it to a file: a''b.

The following bash code used to get the job done for me ...

line="a'b"
echo "${line//\'/\'\'}" > out.txt

... until today when I discovered diverging outputs depending on the version of bash being used:

  • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu): a''b
  • GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu): a\'\'b

I tried to modify the line above in numerous ways but was unable to have it produce the same output a''b in both bash shells.

I ended up using a combination of printf and sed instead,

echo "$(printf $line | sed "s/'/''/g")" > out.txt

but I am still wondering if the job can be gotten done with a more concise pure bash expression. Can it?

I have a variable, e.g. a'b, containing a single quote which I need to replace by two single quotes before writing it to a file: a''b.

The following bash code used to get the job done for me ...

line="a'b"
echo "${line//\'/\'\'}" > out.txt

... until today when I discovered diverging outputs depending on the version of bash being used:

  • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu): a''b
  • GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu): a\'\'b

I tried to modify the line above in numerous ways but was unable to have it produce the same output a''b in both bash shells.

I ended up using a combination of printf and sed instead,

echo "$(echo $line | sed "s/'/''/g")" > out.txt

but I am still wondering if the job can be gotten done with a more concise pure bash expression. Can it?

Source Link
Bastian
  • 161
  • 1
  • 3

bash: Replace single quote by two quotes in string

I have a variable, e.g. a'b, containing a single quote which I need to replace by two single quotes before writing it to a file: a''b.

The following bash code used to get the job done for me ...

line="a'b"
echo "${line//\'/\'\'}" > out.txt

... until today when I discovered diverging outputs depending on the version of bash being used:

  • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu): a''b
  • GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu): a\'\'b

I tried to modify the line above in numerous ways but was unable to have it produce the same output a''b in both bash shells.

I ended up using a combination of printf and sed instead,

echo "$(printf $line | sed "s/'/''/g")" > out.txt

but I am still wondering if the job can be gotten done with a more concise pure bash expression. Can it?