Skip to Main Content
Unix in a Nutshell, 4th Edition
book

Unix in a Nutshell, 4th Edition

by Arnold Robbins
October 2005
Intermediate to advanced content levelIntermediate to advanced
908 pages
46h 42m
English
O'Reilly Media, Inc.
Content preview from Unix in a Nutshell, 4th Edition

Variable Substitution

ksh93 provides structured variables, such as pos.x and pos.y. To create either one, pos must already exist, and braces must be used to retrieve their values. Names beginning with .sh are reserved for use by ksh.

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.

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, and in ksh93 if a variable name contains periods.

${ 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.

${#@}

Same.

${ 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.

In ksh93 and Bash:

${!prefix ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning the Unix Operating System, 5th Edition

Learning the Unix Operating System, 5th Edition

John Strang, Grace Todino, Jerry Peek
UNIX and Linux System Administration Handbook, 5th Edition

UNIX and Linux System Administration Handbook, 5th Edition

Trent R. Hein, Evi Nemeth, Garth Snyder, Ben Whaley, Dan Mackin

Publisher Resources

ISBN: 0596100299Errata Page