Allen_sHH
Allen_sHH

Reputation: 11

How to read full file name in shell

I want to read the full file name, but it always auto separator by space

#!/bin/dash

for file in `ls | grep -E '(jpg)$' | sed 's/\.jpg//g'`
do  
    echo ${file}
done

This will work when the file name do not contain space or other symbol. But it do not work when the file name like : A B C.jpg This will print

A
B
C

It sepator the file name by space, how can i avoid this situation

Upvotes: 1

Views: 30

Answers (1)

dan
dan

Reputation: 5251

Unquoted command substitutions are split on IFS (white space by default).

You can use shell glob expansion, and shell suffix removal:

for file in *.jpg; do
    [ -f "$file" ] || continue
    name=${file%.jpg}
    echo "$name"
done

*.[Jj][Pp][Gg] for a case insensitive match (bash also has shopt -s nocasematch).

Upvotes: 2

Related Questions