Looking to shorten method of parsing arguments in BASH I have here a working method of parsing input arguments however it seems redundant. I would like to shorten this code so it doesn't appear so repetitive.

tool.sh &lt;file-to-use&gt; 
tool.sh &lt;file-to-use&gt; -o &lt;output-file-name&gt; 

# If arguments are set and equals "-o" then set APP_NAME
if [[ -n $3 ]] &amp;&amp; [[ $2 = "-o" ]]; then 
    APP_NAME=$3

# If argument is set and equals anything besides "-o" throw error and show help menu
elif [[ -n $2 ]] &amp;&amp; [[ $2 != "-o" ]]; then
    echo "ERROR: Synatx error."
    bash $0 -h 
    exit

# If "-o" is set but they did not give a output name throw error and display help menu
elif [[ -z $3 ]] &amp;&amp; [[ $2 = "-o" ]]; then
    echo "ERROR: Did not specify output file name."
    bash $0 -h 
    exit  
fi
Use the ${variable?error message} syntax to error on empty arguments.  Use shift to remove parsed arguments from the positional parameters, so that the next unseen argument is always $1.  If there are any arguments left at the end, there were too many.

When a message isn't enough, you can use trap … EXIT to call a function on error.  Remember to remove the trap after parsing and at the very start of your handler function.

This version isn't any shorter than yours but it's more generic.  You can add parameters or rearrange their order without altering the numbers in the existing logic.  The error-handling is in one place instead of two.

show_help() { 
 trap - EXIT # omitting this can lead to an infinite loop
 exec $0 -h
 exit
}

trap show_help EXIT
error_usage="Usage: $0 input-file [-o output-file]"
input=${1?$error_usage} &amp;&amp; shift
[[ $1 = -o ]] &amp;&amp; output=${2?$error_usage} &amp;&amp; shift 2
[[ -n $1 ]] &amp;&amp; echo $error_usage &amp;&amp; exit 0
trap - EXIT

echo "input=$input output=$output"


Rather than invoking ourselves to display help, I'd just put the help logic in the show_help function.