2

What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts?

For example:

filename=/etc/hosts

if [ -r "$filename" ] && [ -s "$filename" ]; then
    md5sum $filename
else
    echo "$filename cannot be processed"
fi

and

filename=${1:-/etc/hosts}

if [ -r "$filename" ] && [ -s "$filename" ]; then
    md5sum $filename
else
    echo "$filename cannot be processed"
fi
Mat
  • 52,586

1 Answers1

10

filename=${1:-/etc/hosts} assigns value /etc/hosts to variable filename if $1 is not set or null.

From GNU bash manual:

${parameter:-word}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

Mat
  • 52,586
cuonglm
  • 153,898