Rory's Blog
Home
Blog
Projects
Facebook
Google+
Github
RSS

Bash Scripting Improvements

07/15/14

Lately, I have been needing to mess around with bash scripts. Moreso than I really like, but "c'est la vie". I figure if I need to code in it, it's better to use the situation as a means of self-improvement than to let myself be annoyed by it. Anyways, today I was thinking about the best way to do multiline comments in bash, and I found this bit of advice:

: '
This is a test comment
author foo bar
Released under GNU
'

Source.

As the source explains, ":" is a built-in command that does nothing, but that's not what I found interesting. I did not know that unterminated quotes would continue a command in bash (csh complains about an "Unmatched '.", futher proof of csh horribleness).

So, it occurred to me, why not get in the habit of putting a usage string at the top of each bash script I write. Sort of like a docstring.

scriptname_usage_string='
USAGE: ./scriptname [OPTIONS....] ARGUMENT
'

And then, if necessary or wanted, I can print scriptname_usage_string when the program terminates due to incorrect usage.

echo -e "$scriptname_usage_string"
USAGE: ./scriptname [OPTIONS....] ARGUMENT

Now I can document my bash scripts better and have a string to print to a user if necessary. Neat, hopefully it turns out to be quite useful.