Quote:
Originally Posted by man bash
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat‐
tern against its value is replaced with string. If Ipattern
begins with /, all matches of pattern are replaced with string.
Normally only the first match is replaced. If pattern begins
with #, it must match at the beginning of the expanded value of
parameter. If pattern begins with %, it must match at the end
of the expanded value of parameter. If string is null, matches
of pattern are deleted and the / following pattern may be omit‐
ted. If parameter is @ or *, the substitution operation is
applied to each positional parameter in turn, and the expansion
is the resultant list. If parameter is an array variable sub‐
scripted with @ or *, the substitution operation is applied to
each member of the array in turn, and the expansion is the
resultant list.
|
So, to replace all, not just the first, the substitution becomes ${filename//-/_}
Code:
robbak@robbak-PC:~/testdir$ ls
file-one file-three file-two with-space-five-mess with space-four
robbak@robbak-PC:~/testdir$ for filename in *; do mv "$filename" "${filename//-/_}"; done
robbak@robbak-PC:~/testdir$ ls
file_one file_three file_two with_space_five_mess with space_four
robbak@robbak-PC:~/testdir$