O'Reilly logo

Linux in a Nutshell, 6th Edition by Robert Love, Stephen Figgins, Ellen Siever, Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Variable Substitution

No spaces should be used in the following expressions. The colon (:) is optional; if it’s included, var must be nonnull, as well as set.

Variable expression

Description

var= value ...

Set each variable var to a value.

${ var}

Use value of var; braces are optional if var is separated from the following text. They are required for array variables.

${ var:- value}

Use var if set; otherwise, use value.

${ var:= value}

Use var if set; otherwise, use value and assign value to var.

${ var:? value}

Use var if set; otherwise, print value and exit (if not interactive). If value isn’t supplied, print the phrase “parameter null or not set.”

${ var:+ value}

Use value if var is set; otherwise, use nothing.

${# var}

Use the length of var.

${#*}, ${#@}

Use the number of positional parameters.

${ var# pattern}

Use value of var after removing pattern from the left. Remove the shortest matching piece.

${ var## pattern}

Same as # pattern, but remove the longest matching piece.

${ var% pattern}

Use value of var after removing pattern from the right. Remove the shortest matching piece.

${ var%% pattern}

Same as % pattern, but remove the longest matching piece.

${! prefix*}, ${! prefix@}

List of variables whose names begin with prefix.

${ var: pos}, ${ var: pos: len}

Starting at position pos (0-based) in variable var, extract len characters, or rest of string if no len. pos and len may be arithmetic expressions.

${ var/ pat/ repl}

Use value of var, with first match of pat replaced with repl.

${ var/ pat}

Use value ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required