Vik
Vik

Reputation: 9289

Reading the full file name using shell script

I have this code:

for dir in "/Users/vskumar/Dropbox/alexa"/*
do
  echo $dir
  value=`cat $dir`
  echo value
done

and the first file in the directory has the name as below:

amazon_alexa_february_7__2017_at_0504pm (2).txt

The output I get is:

/Users/vskumar/Dropbox/alexa/amazon_alexa_february_7__2017_at_0504pm (2).txt
cat: /Users/vskumar/Dropbox/alexa/amazon_alexa_february_7__2017_at_0504pm: No such file or directory
cat: (2).txt: No such file or directory

Basically, it is treating everything after space as another file name. How to fix it?

Upvotes: 1

Views: 39

Answers (1)

codeforester
codeforester

Reputation: 42979

You need to use double quotes around your variables to prevent word splitting and globbing:

for dir in "/Users/vskumar/Dropbox/alexa"/*
do
  echo "$dir"
  value=`cat "$dir"`
  echo value
done

Upvotes: 2

Related Questions