1

I have a script on my mac where I'm trying to loop on the txt files inside a subfolder of my current location. This is myscript.sh:

cd /Users/Desktop/
for entry in $(pwd)/txt/*.txt 
do 
echo $entry
done

this prints a string of the path including *txt in it like /Users/Desktop/txt/*.txt I want to loop on the .txt files so I expect the echo $entry to print the names of the txt files but instead it prints /Users/Desktop/txt/*.txt

Tak
  • 529
  • @Kusalananda I've updated my question. – Tak Mar 30 '17 at 08:31
  • for entry in $(pwd)/txt/*.txt; do echo $(pwd)/txt/${entry}; done – Kamaraj Mar 30 '17 at 08:32
  • for mac.. check this post http://unix.stackexchange.com/questions/156534/bash-script-error-with-strings-with-paths-that-have-spaces-and-wildcards

    http://stackoverflow.com/questions/31797856/loop-through-files-in-mac-terminal

    – Kamaraj Mar 30 '17 at 08:35
  • As an aside, you should use double quotes in echo "$entry". See also http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-value – tripleee Mar 30 '17 at 08:50

1 Answers1

2

When you give the shell a globbing pattern that doesn't match any file names, the globbing pattern will not be expanded. In your case, this means that the echo in the loop outputs the pattern itself.

Alternative implementation of your script:

cd /Users/Desktop || exit 1

for entry in txt/*.txt; do 
    test -e "$entry" && echo "$entry"
done

This implementation will exit with a non-zero exit code if the cd fails. It will then not use pwd since it's unnecessary. It will get a list of names matching the pattern and will iterate over these. In each iteration, it tests to make sure there is actually something in the filesystem that has that name before outputting the name to standard output.

If you want the echo inside the lop to output the full path of the files, use echo "/Users/Desktop/$entry", or even better:

dir="/Users/Desktop"
cd "$dir" || exit 1

for entry in txt/*.txt; do 
    test -e "$entry" && printf '%s/%s\n' "$dir" "$entry"
done
Kusalananda
  • 333,661
  • thanks, but why it doesn't match? although the path is correct and there are some txt files in it? – Tak Mar 30 '17 at 08:36
  • If you had files with an .txt suffix in the txt directory of your current working directory, then the paths of those files would be shown. The current working directory (returned by pwd) is the directory where you're currently at, not where the script lives. – Kusalananda Mar 30 '17 at 08:40
  • I've updated my question as in the beginning of the script I did cd but the echo prints the correct directory, so I'm not sure why it's not working? – Tak Mar 30 '17 at 08:44
  • That's what pwd does. The shell will resolve relative paths from the script's current directory anyway so $(pwd)/ should just be replaced with nothing. Or better yet take out the cd before the loop and just say for entry in /Users/Desktop/txt/*.txt (if you really genuinely have a directory named like that -- aren't you missing the user's account name?) – tripleee Mar 30 '17 at 08:49