What is usage() in shell scripting?

It's a just a convention. When something is wrong with the values supplied on the command line, people often use a function called usage() to tell you the problem/the values expected. For example:

#!/bin/sh
if [ $# -ne 1 ] ; then
    usage
else
    filename=$1
fi
...

When you check the arguments sent to the program, you'll sometimes have to notify the user that they failed the command.

For example, if you expect your program to be called with myprogram filename, then you will call usage if there is no parameter or more than 1 parameter.

Instead of having the same message at several locations in your code with the content of usage, it's a better practice to do only one function.

Tags:

Linux

Unix

Shell