bash - Shell script: Whitespace in file names -
i have shell script processes files. problem there might white spaces in file names, did:
#!/bin/sh file=`echo $file | sed -e 's/[[:space:]]/\\ /g'` cat $file so variable file file name passed in other program. may contain white spaces. used sed escape white space \ in order make command line utilities able process it.
the problem doesn't work. echo $file | sed -e 's/[[:space:]]/\\ /g' works expected, when assigned file, escape char \ disappeared again. result, cat interpret more 1 arguments. wonder why behaves this? there anyway avoid it? , if there're multiple white spaces, some terrible file.txt, should replaced some\ \ \ terrible\ \ file.txt. thanks.
don’t make more complicated is.
cat "$file" that’s need. note quotes around variable. prevent variable being expanded , split @ whitespace. should write shell programs that. put quotes around variables, unless want shell expand them.
for in $pattern; that ok.