Adding Flags to Bash Scripts

This is a simple way to add flags to your bash script


Adding Flags

Adding flags can be done in many different ways, this is a very simple approach. You should be able to run your scripts like this.

./start.sh -t latest --skip-verification

For the example above, your code should look like this:

skipVerification=f
tag=

while [ "$1" != "" ]; do
    case $1 in
        --skip-verification)   shift
                                skipVerification=t
                                ;;
        -t | --tag)           shift
                                tag=$1
                                ;;
        *)                    echo "Invalid flag"
                                exit 1
                                ;;
    esac
    shift
done

This allows you do add logic with your passed flags later.

if [ "$tag" == "" ]; then
    echo "You must provide a tag";
    exit 1;
fi

if [ $skipVerification == "f" ]; then
    # do not verify
else
    # do verify
fi