Make sure that the script is executable, and run the script by just typing the command, including its path. For example, if the script is called foo and is in the current directory, run
./foo
Given the error message, you are doing something like sh foo. This runs the script under sh, not bash. sh on your machine is a different shell, probably dash, which doesn't support the for loop syntax you used. By running ./foo, your script will be executed by the shell mentioned in the first line, which is bash.
Your script is weird in several places:
Always put double quotes around variable substitutionsAlways put double quotes around variable substitutions:
"$1","$f", etc.That
while [ "$*" != "" ] …loop is a very roundabout way of iterating over the arguments of the script. The simple, idiomatic way isfor x; do count_lines "$x" done
or
for x in "$@"; do
count_lines "$x"
done
- I'm not sure what you're trying to do with
title=$(grep -oPm1 "(?<=<title>)[^<]+" <<< "$0");"$0"is the path to the script, so you're searching the regexp(?<=<title>)[^<]+in the path to the script, which doesn't make much sense.