I have the following bit of code in a bash script I've created. When I run it from a directory with no spaces in the path it works as expected, however if I run from a directory with spaces it fails. I'm pretty sure I need to escape it somehow, but nothing I've tried seems to work.
for file in `pwd`/*
do
echo file:$file
done
By 'fails' I mean that if I run the command from the path "~/dir 1/ test" it would echo the following:
file: ~/dir
file: 1/
file: test
I'd expect it to list the files in the directory (which it does if there's no spaces in the current directory):
file: file 1
file: file 2
file: file 3
Note this is only an issue if the current directory has spaces in, files with spaces in are processed fine.
I've read several similar examples on here such as Why does my shell script choke on whitespace or other special characters? and Looping through files with spaces in the names? but I can't see how (if they are relevant) they apply to my specific example.
it fails
mean? – jsotola Jan 12 '20 at 23:10this is what i am trying to do ... this is what i expect to happen ... this is what actually happens .... this is my question
– jsotola Jan 12 '20 at 23:17echo file:"$file"
– jsotola Jan 12 '20 at 23:22pwd
/* as the array that line generates is what is at fault – Al_ Jan 12 '20 at 23:26for file in "$(pwd)"/* do echo file:"$file" done
– Paulo Tomé Jan 12 '20 at 23:29