« Back to Index

[Bash default variable value for script arguments]

View original Gist on GitHub

Tags: #bash #shell #scripting #cli #terminal #defaults

default variable value for script arguments.bash

VAR=${1:-DEFAULTVALUE}  # assign either $1 or "DEFAULTVALUE" to $VAR

#!/bin/bash
n=3
echo ${!n}

# ./args.sh apple banana cantaloupe dates
#
# would echo "cantaloupe"

#!/bin/bash
n=3
echo ${!n:=foo}

# ./args.sh
#
# would echo "foo"