What does the double slash mean in `${f// /_}`?
I'm learning Bash, and I want to replace space characters with other "non blank" characters. I'm using a for
loop:
for f in *\ *; do mv "$f" "${f// /_}"; done
My question is, why are the double slash and the space in ${f// /_}
? What does ${f// /_}
do?
Solution 1:
Thats a replacement pattern using bash
parameter expansion.
In ${f// /_}
:
The double slashes
//
are for replacing all occurrences of space with_
, if you put one slash/
, only first space is going to be replacedThe space is there because you are replacing space (with underscore)
So the pattern basically is:
${parameter//find/replace}
Check man bash
to get more idea.
To get to the Parameter Expansion
section of man bash
at once:
LESS=+/'Parameter Expansion' man bash
Solution 2:
The section "{f// /_}
means replace every space with and underscore.
This is using Bash parameter expansion, the variable f
defined in the for f in *\ *;
will be run through for every match of shell expansion (globbing). Each time the filename found will become the value $f
.
The parameter expansion works with the //
meaning every occurrence of the character following //
(space in this example), should be replaced by the character after /
(underscore in this example).