In a section of a script I am writing, I output a list of paths into a temporary text file for use later with cd. I am using sed to replace spaces in directory names with '\ ' so that I also make the script work with spaces in dir names. My command is given below:
cat temp.txt | sed 's/ /\\ /g'
This has the desired output of replacing spaces with '\ ' but when I try to cd later it ignores the second word and says that example\ does not exist. Any ideas how to fix this?
My whole script is on pastebin here if you want to look at it: http://pastebin.com/6jmnKDAd
This is from a backup before I added sed
To complement Lorenzo's answer, if you have a bunch of newline-separated pathnames in a file:
while IFS= read -r path; do
cd "$path"
do stuff
done < temp.txt
It is critical to quote "$path"
to prevent the shell's word splitting