0

I'm trying to use filenames loaded from a directory.

It works fine but once the filename has whitespaces it doesn't work.

I'll demonstrate:

Assume your in folder scriptExample with three filename dummy file one , dummy file two , dummy file three.

My scripts performs simple cat command for each file

#!/bin/bash
for i in *
        do    
                cat $i
        done

However when you run this you get:

cat: dummy: No such file or directory
cat: file: No such file or directory
cat: one: No such file or directory
cat: dummy: No such file or directory
cat: file: No such file or directory
cat: three: No such file or directory
cat: dummy: No such file or directory
cat: file: No such file or directory
cat: two: No such file or directory
#!/bin/bash
for i in *
    do
        cat $i
    done

1 Answer 1

2

You can simply surround your variable ($i) with double quotes:

#!/bin/bash
for i in *
do
    cat "$i"
done

See also:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.