Reputation: 699
I have a simple script:
for photo in $full_path/*.jpg; do
$b_photo=$(basename $photo)
echo $b_photo
done
it's simplified form of what I'm trying to do, but this gives me error like this:
./foo.sh: line 334: =foobar.jpg: command not found
Upvotes: 0
Views: 1002
Reputation: 65854
In addition to the problem pointed out by fedorqui, basename $photo
will go wrong if $photo
contains a space:
$ photo="directory/file name"
$ basename $photo
file
It will also go wrong if $photo
starts with a -
:
$ photo="-directory/file"
$ basename $photo
basename: illegal option -- d
usage: basename string [suffix]
basename [-a] [-s suffix] string [...]
You could write:
for photo in "$full_path"/*.jpg; do
basename -- "$photo"
done
but a simpler way to carry out this task (printing file names in a directory that match a pattern to standard output, one per line) would be to switch to the directory and use printf
:
{ cd -- "$full_path" && printf "%s\n" *.jpg }
Upvotes: 3
Reputation: 290145
Assign variables with var=$(command)
and use them with $var
(or, better, ${var}
):
for photo in $full_path/*.jpg; do
b_photo=$(basename $photo) <------ b_photo=$() instead of $b_photo=$()
echo $b_photo ^
done
Note you can directly write:
for photo in $full_path/*.jpg; do
basename "$photo"
done
Upvotes: 5