I am attempting to write a bash script that operates on each directory in a subset. Unfortunately the path contains spaces, and I am unable to change the name.
The for
loop insists on breaking at each space; I have tried dozens of variants, but am unable to come up with a solution.
A simplified version is below.
#!/bin/bash
SOURCE="/Volumes/Time Machine Backups/Backups.backupdb/Zaphod"
for file in `ls -d "$SOURCE"/201*`
do
echo "File: $file"
done
Can anyone suggest how I can get every entry e.g. /Volumes/Time Machine Backups/Backups.backupdb/Zaphod/2017-06-30-215735
in a separate variable.
sudo
tocd
– Milliways Jul 06 '17 at 03:12cd "$SOURCE"
. And, of course, `"$filename". It is a good practice to quote expansions, specially if the do contain spaces. – Jul 06 '17 at 05:24cd $SOURCE
doesn't work, you'd needcd "$SOURCE"
. And parsingls
only works if the files in that directory don't contain any special characters. Don't parse the output of ls and put double quotes around variable substitutions. – Gilles 'SO- stop being evil' Jul 06 '17 at 22:54