Reminder: Do not start with an integer for a variable name in Bash

Written by - 0 comments

Published on - Listed in Shell Linux Unix Coding Bash


Just had a weird situation, where I wrote a quick and dirty shell script to output the current amount of deferred mails due to smtp error 421 and send an alert. 

I tried to count the different responses, each smtp response holding an integer as value:

421ips=$(mailq | grep -i -c "refused to talk to me: 421 4.7.1 Intrusion prevention active")
-bash: 421ips=11: command not found

421notaccepting=$(mailq | grep -i -c "421 4.3.2 System not accepting network messages")
-bash: 421notaccepting=11: command not found

421rate=$(mailq | grep -i -c "Our system has detected an unusual rate of 421-4.7.0 unsolicited mail")
-bash: 421rate=6: command not found

I was wondering what the hell just happened until I tried a plain simple variable name:

BLA="$(mailq | grep -i -c "refused to talk to me: 421 4.7.1 Intrusion prevention active")"

Here no error occurred. And that's the moment it hit me: Of course!
Do not start the variable's name with an integer (in this case 421). D'oh!

Distractions, distractions... My mind has been wandering all day.

Actually if I would have used vim with the syntax on command, I would have seen that something's off:

See the $4 turning blue while the rest doesn't? That would have given me a clue.
But as I just created the file, vim didn't know (yet) that it'll be a shell script and therefore couldn't show the syntax highlighting. Only after a close (and save) and reopen with vim the syntax highlights are shown.


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.