Skip to Content
Mac OS X in a Nutshell
book

Mac OS X in a Nutshell

by Jason McIntosh, Chuck Toporek, Chris Stone
January 2003
Intermediate to advanced
832 pages
32h 40m
English
O'Reilly Media, Inc.
Content preview from Mac OS X in a Nutshell

Variables

This subsection describes the following:

  • Variable substitution

  • Variable modifiers

  • Predefined shell variables

  • Formatting for the prompt variable

  • Sample .cshrc file

  • Environment variables

Variable substitution

In the following substitutions, braces ({ }) are optional, except when needed to separate a variable name from following characters that would otherwise be considered part of the name:

Variable

Description

${ var }

The value of variable var.

${ var [ i ]}

Select word or words in position i of var. i can be a single number, a range m-n, a range -n (missing m implies 1), a range m- (missing n implies all remaining words), or * (select all words). i also can be a variable that expands to one of these values.

${# var }

The number of words in var.

${#argv}

The number of arguments.

$0

Name of the program.

${argv[ n ]}

Individual arguments on command line (positional parameters); 1≤n≤9.

${ n }

Same as ${argv[ n ]}.

${argv[*]}

All arguments on command line.

$*

Same as {$argv[*]}.

$argv[$#argv]

The last argument.

${? var }

Return 1 if var is set, 0 if var is not set.

$$

Process number of current shell; useful as part of a filename for creating temporary files with unique names.

${?name}

Return 1 if name is set, 0 if not.

$?0

Return 1 if input filename is known, 0 if not.

Examples

Sort the third through last arguments and save the output in a file whose name is unique to this process:

                     sort $argv[3-] > tmp.$$

Process ...

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

Mac OS X Internals: A Systems Approach

Mac OS X Internals: A Systems Approach

Amit Singh
C++ In a Nutshell

C++ In a Nutshell

Ray Lischner
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar
Optimized C++

Optimized C++

Kurt Guntheroth

Publisher Resources

ISBN: 0596003706Supplemental ContentCatalog PageErrata