I am trying to write a shell script which will read a file and counts the number of vowels in the file. The code which read the file is as follows -

while read -n 1 c
  l=$(echo $c | tr [:upper:] [:lower:])
  [[ "$l" == "a" || "$l" == "e" || "$l" == "i" || "$l" == "o" || "$l" == "u" ]] && (( v++ ))
done < $file

When I run the script, getting the error -
read: 22: Illegal option -n where 22 is the line number.
I am confused why -n option is not working in the script whereas read -n 1 c command is working fine from terminal.
Any suggestion is appreciated.
Thanks in advance.

5 Years
Discussion Span
Last Post by java_programmer

The only reason I can think about is that the shells are different. What is in the #! line of the script, and what system you are running at?


The script is run in bash shell.
The line with #! is -


Also when I run the command echo $SHELL it outputs /bin/bash i.e. both command and script running in same shell. But the command is working, not the script.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.