Bash parameter extension $ {parameter / pattern / string} with quotes

This is my first post on SO (long time lurker), so I apologize in advance for the many "faux pas" and other mistakes that I am definitely going to make.

I searched Google for a long time, trying to find an answer to how quotes are parsed when inside a parameter extension inside double quotes, and it seems that I either have the wrong keywords or a very distorted mind to try this.

For example, if I have a type string It a complicated string, I would like to convert this string to a sequence It'\' a complicated stringusing the bash extension ${parameter/pattern/string}. I know that I can achieve this result using one of many other built-in or external tools (I really like sed), but this question really is understanding what is going on in the bash mind so that I can put my mind to ease.

The bash link doesn't seem to indicate what happens in this special case when describing its “template” and the closest question about SO doesn't seem to work in my case:

$ echo "$str"
It a complicated string
$ echo "${str//'/'\''}"
> ^C
$ echo "${str//'/'\''}"
> ^C
$ echo "${str//\'/'\''}"
> ^C
$ echo "${str//\'/\'\''}"
> ^C
$ echo "${str//\'/\'\'\'}"
It\'\'\ a complicated string
$ echo "${str//\\'/\'\'\'}"
It a complicated string
$ echo "${str//\\'/\\'\'\'}"
It a complicated string
$ echo "${str//\\'/\\'\\'\'}"
It a complicated string
$ echo "${str//\\'/\\'\\'\\'}"
It a complicated string
$ echo "${str//\\\'/\\'\\'\\'}"
> ^C
$ echo "${str//\\\'/\\\'\\'\\'}"
It a complicated string
$ echo "${str//\\\'/\\\'\\\'\\'}"
> ^C
$ echo "${str//\\\'/\\\'\\\'\\\'}"
It a complicated string

( > ^C , , , , Ctrl-C.)

- , , bash? , , .:)

EDIT:

, , :

$ q=\'
$ echo "${str//\'/$q\'$q}"
It'\' a complicated string

sputnick, :

$ echo "${str//\047/\047\\\047\047}"
It a complicated string
$ echo "${str//\047/\047\047}"
It a complicated string
$ echo "${str//\'/\047\'\047}"
It\047\'\047s a complicated string
$ echo "${str//\'/\047\047\047}"
It\047\047\047s a complicated string
$ echo "${str//'/\047\047\047}"
> ^C
$ echo "${str//\047/\047\047\047}"
It a complicated string
$ echo "${str//\047/\047\\\047\047}"
It a complicated string

EDIT2:

-, , bash 4.1 4.2 bash 4.3. .

+4
3

( ):

echo "${str//\'/\'\\\'\'}"

, . .


bash 4.3:

$ str="It a complicated string"
$ echo "${str//\'/\'\\\'\'}"
It'\' a complicated string
$ echo ${str//\'/\'\\\'\'}
It'\' a complicated string

bash 3.2 .

+2

, , ascii : \047

.

man 7 ascii
+1

I can’t explain what bash does exactly, but like @sputnick my suggestion is simply not to play the game.

q=\'
echo "${str//\'/$q\'$q}"
+1
source

All Articles