sed and escaping & in something like: echo $y | sed 's/&/_/g'
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
So you want to replace spaces and '&' chars: to do what you want, you need a separate sed cmd for each char you want to replace becuase you are replacing them with different replacement chars.
t1='apples & oranges'
t2=`echo $t1|sed 's/&/and/'`
apples and oranges
t3=`echo $t2|sed 's/ /_/g'`
for i in *
if [ -d "$i" ] # if * is a directory
cd "$i" # descend into the directory
for y in *
# tempa=$(echo $y | sed 's/ /_/g')
tempa=$(echo $y | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g')
if [ "$y" != "$tempa" ]
mv "$y" "$tempa"
if [ -d "$tempa" ] # if this is also a directory, call this program
sr; # this is the name of THIS program, must be in your PATH
# tempa=$(echo $i | sed 's/ /_/g')
tempa=$(echo $i | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g')
if [ "$i" != "$tempa" ]
mv "$i" "$tempa"