What is a good way to output help text for a bash script to get the columns lines to be lined up properly?
something like:
Usage: mycommand [options]
-h| --help this is some help text.
this is more help text.
-1|--first-option this is my first option
-2|--second-option this is my second option
There are multiple ways to show script usage inside of your Bash script. One way is to check if the user has supplied the -h or --help options as arguments as seen below. #!/bin/bash # check whether user had supplied -h or --help .
The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching. Follow this answer to receive notifications.
From Advanced Bash-Scripting Guide: $$ is the process ID (PID) of the script itself. $BASHPID is the process ID of the current instance of Bash.
To format date in YYYY-MM-DD format, use the command date +%F or printf "%(%F)T\n" $EPOCHSECONDS . The %F option is an alias for %Y-%m-%d . This format is the ISO 8601 format.
I like to use cat
for this:
usage.sh:
#!/bin/bash
cat <<EOF
Usage: $0 [options]
-h| --help this is some help text.
this is more help text.
-1|--first-option this is my first option
-2|--second-option this is my second option
EOF
This will output:
Usage: usage.sh [options]
-h| --help this is some help text.
this is more help text.
-1|--first-option this is my first option
-2|--second-option this is my second option
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With