Show bash script documentation trick
Bash scripts are the bread and butter when organizing computer stuff. After writing so many script I don’t remember everything I did. It is nice to have comments or some other way of knowing what it is that certain script does.
There are many solutions, but I tend to use comments (lines starting with #) and usually my bash scripts start something like this:
#!/usr/bin/env bash
#
# Converts markdown files to HTML presentations using pandoc
#
# Example usage:
#
# md2html.sh name-of-file.md
#
head -8 "$0"
The above snippet ensures that the head of the file is always shown when the script is ran. The head -N "$0" line is where the magic happens. I just replace N with the number of lines that I want to show.
Whenever the script runs, the first thing it does is to display the header documenting the script. If you scripts require the passing of arguments then running the script without parameters will show the proper way to use the script.
Neat.