6 ms·
Or just a multiline string: #!/bin/bash USAGE="my-script — does one thing well Usage: my-script <input> <output> Options: <in
by pavon 6y ago
Or just a multiline string:
#!/bin/bash
USAGE="my-script — does one thing well
Usage:
my-script <input> <output>
Options:
<input> Input file to read.
<output> Output file to write. Use '-' for stdout.
-h Show this message.
"
help() {
echo "$USAGE"
}
This is my standard approach which is cleaner for putting the documentation at the very top of the file like the linked article.
- sillysaurusx 6y agoThank you! I had no idea that multiline strings were valid bash.
- rkangel 6y agoIt's the same logic that allows you to type: git commit -m "First line of commit Second line of commit" That's a multi-line string in bash.
- tuatoru 6y agoThis is one (useful!) interpretation of "code should be self-documenting". Just put the documentation into strings.
- GordonS 6y agoWoah, I had no idea multiline strings were even a thing in Bash, I've been using heredocs for help messages since forever! Do you know if these are portable?
- pavon 6y agoAs far as I know. The POSIX spec[1] simply declares that in single quotes all characters will be preserved exactly, except for single quotes, which aren't allowed. Likewise, for double quotes, except that it also performs expansions ($) and allows escaping (\). [1]https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02 https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
- Anthony-G 6y agoThat's exactly what I do. For others who were not aware that multi-line strings can be used, this is POSIX-compatible (most of my shell scripts are executed by `dash`).