From what I’ve read at tldp.org, the double parenthesis construct (( ... )) allows you to reference bash variables without the $ prefix. However, when attempting to reference the argument count variable $#, the script refuses to work with a
bash: ((: i < # : syntax error: operand expected (error token is "# ")
oddly enough, when the hash is prefixed, the script works fine.
For your reference, here’s the working code:
fun(){
i=0
while (( i < $# )); do
echo $1;
shift
done
}
My question is: How do I know when it is correct to include the prefix and when it is not?
You can forgo the
$as long as it’s not a SPECIAL PARAMETER. Also, if you are doing PARAMETER EXPANSION inside(( ))you also need the$