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.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder