Passing quoted string arguments to a bash script -
i have not been able find solution seemingly simple problem here or anywhere else. want paths of files in nested directory, add quotes around them, , loop through them in bash script. directory names , files can have white spaces in them. far nothing tried works properly. quoted path strings broken @ spaces.
test.sh
for var in "$@" echo "$var" done
i tried reading file 1 path per line, both single , double quotes:
find "nested directory spaces" -type f | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' '\n' > list.txt # double quotes ./test.sh `cat list.txt` find "nested directory spaces" -type f | sed -e 's/^/'\''/g' -e 's/$/'\''/g' | tr '\n' ' ' > list.txt # single quotes ./test.sh `cat list.txt`
and command substitution space between quoted paths, single , double quotes:
./test.sh `find "nested directory spaces" -type f | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' ' '` # double quotes ./test.sh `find "nested directory spaces" -type f | sed -e 's/^/'\''/g' -e 's/$/'\''/g' | tr '\n' ' '` # single quotes
simply echoing quoted path command line gives desired result. missing in script can resolve arguments complete strings?
do instead:
find "nested directory spaces" -type f -exec ./test.sh {} +
this call test.sh
multiple arguments, without splitting spaces in filenames.
if version of find
doesn't support +
, can use \;
instead, call ./test.sh
once each argument.
for example, given script:
#!/bin/sh echo start i; echo file: "$i" done
the difference between +
, \;
:
$ find a\ b.txt date.txt -exec ./test.sh {} + start file: b.txt file: date.txt $ find a\ b.txt date.txt -exec ./test.sh {} \; start file: b.txt start file: date.txt
Comments
Post a Comment