Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Script "Usage" output formatting

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
like image 346
flamusdiu Avatar asked Oct 22 '13 02:10

flamusdiu


People also ask

How do I see bash script usage?

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 .

What does %% mean in bash?

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.

What is $$ in bash script?

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.

How do I print Yyyymmdd format in Unix?

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.


1 Answers

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
like image 172
imp25 Avatar answered Oct 06 '22 08:10

imp25