Search code examples
bashsolaris

What does ${1:-${variable_name}} mean in solaris?


I have a solaris shell script with a function with given below code:

    function get_prev_bday()
{
    local _HOUR_CUTOFF=""
    local _YYYYMMDD="`date '+%Y%m%d'`"

    while [ ! -z "$1" ]; do
        case $1 in
            -d | --day )  shift; _YYYYMMDD=${1:-${_YYYYMMDD}}
            ;;
            -h | --hour)  shift; _HOUR_CUTOFF=${1:-${_HOUR_CUTOFF}}
                                 _HOUR_CUTOFF="`echo $_HOUR_CUTOFF | sed -e 's/^0//'`"
            ;;
            *) echo -e "$FUNCNAME --\nUsage: get_prev_bday:
                    -d YYYYMMDD (optional, defaulted to today)
                    -h hour_cutoff (optional, used for current day late night run only)
                  "
               return 1
        esac
        shift
    done

The code above is not complete but I am interested in this part only! So I have this function get_prev_bday which takes -d (if specified, this is optional) as date on which we want to find previous business day on? In the while loop case when $1 is -d, it should assign _YYYYMMDD value of -d tag. But I am not able to figure out what the code already there is doing there? Can you guys help me by telling me what does this expression in code do?

_YYYYMMDD={1:-${_YYYYMMDD}}

Solution

  • This is a form of parameter substitution. It means take the value of the variable $1, but if $1 is not set then use the value ${_YYYYMMDD}. So the expression defaults to ${_YYYYMMDD} if $1 is not set.

    ${parameter-default}, ${parameter:-default}

    If parameter not set, use default.