Yes, it is a pattern replacement in shell parameter expansion as:
${parameter/pattern/replacement}
But if the first character after the first /
is either /
or #
or %
it has the special meaning of all
(repeated), start
and end
.
with:
$ str='one_#two_two_three_one'
A single /
will replace the first instance. The first instance of one
:
$ echo "${str/one/x-x}"
x-x_#two_two_three_one
Or the first instance of two
:
$ echo "${str/two/x-x}"
one_#x-x_two_three_one
The instance of one
at the end:
$ echo "${str/%one/x-x}"
one_#two_two_three_x-x
All repetitions of two
:
$ echo "${str//two/x-x}"
one_#x-x_x-x_three_one
The instance of one
at the start:
$ echo "${str/#one/x-x}"
x-x_#two_two_three_one
An string that start with #
(quote the #
):
$ echo "${str/\#two/x-x}"
one_x-x_two_three_one
But if you leave the # (unquoted) alone, the replacement is set at the beginning of the variable:
$ echo "${str/#/====}"
====one_#two_two_three_one
Furthermore, if the parameter is an array, the replacement is done on all elements:
$ str=( one two three )
$ echo "${str[@]/#/==}"
==one ==two ==three
#
and%
are part of the pattern while//
is a different operator from/
and use the same patterns. You can havepattern='#x'; echo "${var/$pattern}"
(or${var//$pattern}
), butpattern=/x; echo "${var/$pattern}"
is not the same asecho "${var//x}"
. Note that#
and%
can be combined inzsh
, but notbash
norksh
. – Stéphane Chazelas Oct 06 '18 at 22:23