Variable Modifiers

Except for $? var, $?0, $# var, $% var, $#, $$, $?, $!, $_, and $<, the variable substitutions in the preceding section may be followed by one of these modifiers (when braces are used, the modifier goes inside them):

:r

Return the variable’s root (the portion before the last dot).

:e

Return the variable’s extension.

:h

Return the variable’s header (the directory portion).

:t

Return the variable’s tail (the portion after the last slash).

:gr

Return all roots.

:ge

Return all extensions.

:gh

Return all headers.

:gt

Return all tails.

:q

Quote a wordlist variable, keeping the items separate. Prevents further substitution. Useful when the variable contains filename metacharacters that should not be expanded.

:x

Quote a pattern, expanding it into a wordlist.

Examples using pathname modifiers

The following table shows the effect of pathname modifiers if the aa variable is set as follows:

    set aa=(/progs/num.c /book/chap.ps)

Variable portion

Specification

Output result

Normal variable

echo $aa

/progs/num.c /book/chap.ps

   

Second root

echo $aa[2]:r

/book/chap

Second header

echo $aa[2]:h

/book

Second tail

echo $aa[2]:t

chap.ps

Second extension

echo $aa[2]:e

ps

   

Root

echo $aa:r

/progs/num /book/chap.ps

Global root

echo $aa:gr

/progs/num /book/chap

Header

echo $aa:h

/progs /book/chap.ps

Global header

echo $aa:gh

/progs /book

Tail

echo $aa:t

num.c /book/chap.ps

Get Unix in a Nutshell, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.