Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Requiring One and Only One Argument In A Bash Function

This is an example function you can add to your .rc files that requires exactly one argument to run. It checks to make sure there's an argument in slot [TODO: Code shorthand span ] otherwise it bails. Once it's past that check, it checks to see if an extra argument was passed in slot [TODO: Code shorthand span ] and bails if that's the case.

Quotes can be used if you need an argument with spaces in it (e.g. ` my _ command "roll tide" ` ).

bash
function my_command () {
    if [ "$1" ]
    then
        if [ "$2" ]
        then
            echo "You passed too many arguments"
        else
            echo "Got: $1 - Do Stuff Here"
        fi
    else
        echo "You need to pass an argument"
    fi
}