Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
If you don't need to learn it by heart, you should at least know where to read about it: it's in the bash manual. – glenn jackman Mar 15 at 14:50
up vote 12 down vote accepted

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 replaced

  • The space is there because you are replacing space (with underscore)

So the pattern basically is:


Check man bash to get more idea.

To get to the Parameter Expansion section of man bash at once:

LESS=+/'Parameter Expansion' man bash
share|improve this answer
In short, its a global search and replace. – glenn jackman Mar 15 at 14:49
@glennjackman for very, very small values of "global". – hobbs Mar 15 at 15:50
Thanks, it's clear now. :D – Pretenderus Mar 16 at 8:22

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).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.