1

Please explain the use of pattern

arg="${arg//\\/\\\\}"

Does it mean exact match?

1
  • You could also have asked me at the time and I'd have added an explanation Apr 23, 2023 at 16:06

1 Answer 1

1

${var//pattern/replacement} is a parameter expansion operator of the ksh93 shell.

It expands to the value of $var where every occurrence of strings matching the pattern have been replaced with replacement.

That operator has been copied by a few shells including zsh, bash and mksh. It is otherwise not a standard sh parameter expansion operator.

The pattern is a glob pattern of the shell for instance where ? matches a single character, * matches any sequence of characters, [abc] match any one of a, b or c characters (and various shells have additional extensions over those).

If a special glob character is quoted like with '*', \*, "*"¹, then it is taken literally. ${var//\?/replacement} for instance replaces question marks instead of every single character.

A backslash must also be quoted like with \\ or '\' or "\\" to represent itself so ${var//\\/\\\\} means expand to the value of $var where each occurrence of backslash has been replaced with two backslashes.


¹ Using \ as opposed to other quoting operators is generally preferable though for the cases where the whole ${...} is within double quotes. If the patterns and/or replacements are derived from expansions, and those expansions are not quoted (like in ${var//$pattern/$replacement} as opposed to ${var//"$pattern"/"$replacement"}, then backslashes must be doubled in ksh93 and bash, but must not in mksh or zsh (at least in their current versions)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .