Search code examples
linuxbashshellunixcygwin

Cygwin bash: read file word by word


I want to read text file word by word. Problem: there are some words containing "/*". Such a word causes script to return files in root directory. I tried:

for word in $(< file)
do
    printf "[%s]\n" "$word"
done

And several other combinations with echo/cat/etc... For this file:

/* example file

I get following output:

[/bin]
[/cygdrive]
[/Cygwin.bat]
...
[example]
[file]

Should be easy but it's driving me nuts.


Solution

  • You need to turn off pathname expansion globbing. Run a new shell with bash -f and try again. See http://wiki.bash-hackers.org/syntax/expansion/globs or dive into the manpage with man bash, maybe do man bash | col -b >bash.txt.