Go to the previous, next section.
Expansion is performed on the command line after it has been parsed. The types of expansions performed are filename expansion, process substitution, parameter expansion, command substitution, arithmetic expansion, brace expansion, and filename generation.
Each word is checked to see if it begins with an unquoted ~
. If
it does, then the word up to a /
is checked to see if it matches
the name of a named directory. If so, then the ~
and the matched
portion are replaced with the value of the named directory. A ~
by itself or followed by a /
is replaced by the value of the
HOME
parameter. A ~
followed by a +
or a -
is replaced by the value of PWD
or OLDPWD
,
respectively.þ
þ
Named directories are typically login directories for users on the
system. They may also be defined if the text after the ~
is the
name of a string shell parameter whose value begins with a /
. In
certain circumstances (in prompts, for instance), when the shell prints
a path, the path is checked to see if it has a named directory as its
prefix. If so, then the prefix portion is replaced with a ~
followed by the name of the directory. The longest match is
preferred.
If a word begins with an unquoted =
and the NO_EQUALS
option is not set, the remainder of the word is taken as the name of a
command or alias. If a command exists by that name, the word is
replaced by the full pathname of the command. If an alias exists by
that name, the word is replaced with the text of the alias. Otherwise
the word is checked up to a /
to see if it is a number or a
-
. If so, the matched portion is replaced with the n'th
directory in the directory stack, where n is the number matched,
or the last directory in the directory stack if a -
is
matched.
Filename expansion is performed on the right hand side of a parameter
assignment, including those appearing after commands of the
typeset
family. In this case, the right hand side will be
treated as a colon-separated list in the manner of PATH
, so that
a ~
or =
following a :
is eligible for expansion.
All such behaviour can be disabled by quoting the ~
, =
, or
the whole expression (but not simply the colon); the NO_EQUALS
option is also respected.
If the option MAGIC_EQUAL_SUBST
is set, any unquoted shell
argument of the form identifier=expression
becomes
eligible for file expansion as described in the previous paragraph.
Quoting the first =
also inhibits this.
Each command argument of the form <(list)
,
>(list)
or =(list)
is subject to process
substitution. In the case of the <
and >
forms, the shell
will run process list asynchronously, connected to a named pipe
(FIFO). The name of this pipe will become the argument to the command.
If the form with >
is selected then writing to this file will
provide input for list. If <
is used, then the file passed
as an argument will be a named pipe connected to the output of the
list process. For example,
paste <(cut -f1 file1) <(cut -f3 file2) | tee >(process1) >(process2) >/dev/null
cut
s fields 1 and 3 from the files file1 and file2
respectively, paste
s the results together, and sends it to the
processes process1 and process2. Note that the file, which
is passed as an argument to the command, is a system pipe so programs
that expect to lseek(2)
on the file will not work. Also note
that the previous example can be more compactly and efficiently written
as:
paste <(cut -f1 file1) <(cut -f3 file2) >>(process1) >>(process2)
The shell uses pipes instead of FIFOs to implement the latter two process substitutions in the above example. þ þ
If =
is used, then the file passed as an argument will be the name
of a temporary file containing the output of the list process.
This may be used instead of the <
form for a program that expects
to lseek(2)
on the input file.
The character $
is used to introduce parameter expansions.
See section Parameters, for a description of parameters.
${name}
${+name}
${name:-word}
${name:=word}
${name:?word}
${name:+word}
${name#pattern}
${name##pattern}
${name%pattern}
${name%%pattern}
${#spec}
${^spec}
RC_EXPAND_PARAM
option for the evaluation
of spec. When this option is set, array expansions of the form
`foo${xx}bar', where the parameter `xx' is set to `(a b
c)', are substituted with `fooabar foobbar foocbar' instead of the
default `fooa b cbar'.þ
þ
þ
þ
${=spec}
SH_WORD_SPLIT
option for the evaluation
of spec. When this option is set, parameter values are split into
separate words using IFS
as a delimiter before substitution.
This is done by default in most other shells.þ
þ
þ
${~spec}
GLOB_SUBST
option for the evaluation of
spec. When this option is set, any pattern characters resulting
from the substitution become eligible for file expansion and filename
generation.If the colon is omitted from one of the above expressions containing a colon, then the shell only checks whether name is set or not, not whether it is null.
If the opening brace is directly followed by an opening parenthesis
the string up to the matching closing parenthesis will be taken as a
list of flags. Where arguments are valid, any character, or the
matching pairs (...)
, {...}
, [...]
,
or <...>
, may be used in place of the colon as delimiters.
The following flags are supported:
o
O
i
o
or O
, makes the sort case-insensitive.
L
U
C
c
${#name}
, count the total number of characters in an
array, as if the elements were concatenated with spaces between
them.
w
${#name}
, count words in arrays or strings; the
s
flag may be used to set a word delimiter.
l:expr::string1::string2:
r:expr::string1::string2:
l
, but pad the words on the right.
j:string:
SH_WORD_SPLIT
option.
s:string:
SH_WORD_SPLIT
) at the
separator string. Splitting only occurs in places where an array
value is valid.
S
${...#...}
and ${...%...}
forms).
Search substrings as well as beginnings or ends of strings.
I:expr:
M
R
B
E
N
A command enclosed in parentheses preceded by a dollar sign, like so:
$(...)
or quoted with grave accents: `...`
is
replaced with its standard output. If the substitution is not enclosed
in double quotes, the output is broken into words using the IFS
parameter. The substitution $(cat foo)
may be replaced by the
equivalent but faster $(<foo)
. In either case, if the option
GLOB_SUBST
is set the output is eligible for filename
generation.
A string of the form $[exp]
is substituted with the value
of the arithmetic expression exp. exp is treated as if it
were within single quotes. See section Arithmetic Evaluation.
A string of the form `foo{xx,yy,zz}bar' is expanded to the
individual words `fooxxbar',
`fooyybar', and `foozzbar'.
Left-to-right order is preserved. This construct may be nested.
Malformed brace expansion expressions, including expressions without a
comma, are left unchanged by the shell.
An expression of the form {x-y}
, where x
and y
are single characters, is expanded to every character between x
and y
, inclusive.
If a word contains an unquoted instance of one of the characters
*
, |
, <
, [
, or ?
, it is regarded as a
pattern for filename generation, unless the NO_GLOB
option is
set. If the EXTENDED_GLOB
option is set, the ^
and
#
characters also denote a pattern; otherwise (except for an
initial ~
, See section Filename Expansion) they are not
treated specially by the shell. The word is replaced with a list of
sorted filenames that match the pattern. If no matching pattern is
found, the shell gives an error message, unless the NULL_GLOB
option is set, in which case the word is deleted; or unless the
NO_NOMATCH
option is set, in which case the word is left
unchanged. In filename generation, the character /
must be
matched explicitly; also, a .
must be matched explicitly at the
beginning of a pattern or after a /
, unless the GLOB_DOTS
option is set. No filename generation pattern matches the files
`.' or `..'. In other instances of pattern matching, the
/
and .
are not treated specially.
*
?
[...]
[^...]
!...
is the same.
<x-y>
x
to y
, inclusive. If
x
is omitted, the number must be less than or equal to y
.
If y
is omitted, the number must be greater than or equal to
x
. A pattern of the form <->
or simply <>
matches
any number.
^x
x
.
x|y
x
or y
.
x#
x
.
x##
x
.
Parentheses may be used for grouping. Note that the |
character
must be within parentheses, so that the lexical analyzer does not think
it is a pipe character. Also note that /
has a higher precedence
than ^
; that is:
ls ^foo/bar
will search directories in `.' except `./foo' for a file named `bar'.
A pathname component of the form (foo/)#
matches a path
consisting of zero or more directories matching the pattern foo.
As a shorthand, **/
is equivalent to (*/)#
. Thus:
ls (*/)#bar
or
ls **/bar
does a recursive directory search for files named `bar'. þ þ
If used for filename generation, a pattern may contain an exclusion
specifier. Such patterns are of the form pat1~pat2
.
This pattern will generate all files matching pat1, but which do
not match pat2. For example, `*.c~lex.c' will match all
files ending in `.c', except the file `lex.c'. This may
appear inside parentheses. Note that ~
has higher precedence
than |
, so that `pat1|pat2~pat3' matches any time that
pat1 matches, or if pat2 matches while pat3 does not.
Note also that any /
characters are not treated specially in the
exclusion specifier, so that a *
will match multiple path
segments if they appear in the pattern to the left of the
~
.þ
þ
Patterns used for filename generation may also end in a list of qualifiers enclosed in parentheses. The qualifiers specify which filenames that otherwise match the given pattern will be inserted in the argument list. A qualifier may be any one of the following:
/
.
@
=
p
*
%
%b
%c
r
w
x
R
W
X
s
S
ddev
l[+-]ct
-
), more than (+
), or
equal to ct.
U
G
uid
u
will be used as a separator and the string
between it and the next matching separator ((
, [
,
{
, and <
match )
, ]
, }
, and
>
respectively; any other character matches itself) will be taken
as a user name and translated into the corresponding user id (e.g.
u:foo:
or u[foo]
for user foo
).
gid
uid
but with group ids or names.
a[+-]n
-
), more than n
days ago (+
), or exactly n days ago.
m[+-]n
-
), more than n
days ago (+
), or exactly n days ago.
c[+-]n
-
), more than
n days ago (+
), or exactly n days ago. If any of
the flags a
, m
, or c
is directly followed by a M
,
w
, h
, or m
(e.g. mh+5
) the check is
performed in units of months (of 30 days), weeks, hours, or minutes
respectively.
L[+-]n
-
), more than n bytes
(+
), or exactly n bytes in length.
^
-
M
MARK_DIRS
option for the current pattern.
T
LIST_TYPES
, for the current pattern (overrides M
).N
NULL_GLOB
option for the current pattern.
D
GLOB_DOTS
option for the current pattern.
More than one of these lists can be combined, separated by commas; the
whole list matches if at least one of the sublists matches (they are
or'ed, the qualifiers in the sublists are and'ed). A /
at the
end of a pattern is equivalent to (/)
.
If a :
appears in a qualifier list, the remainder of the
expression in parentheses is interpreted as a modifier
(See section Modifiers). Note that each modifier must be introduced by a
separate :
. Note also that the result after modification does
not have to be an existing file. The name of any existing file can be
followed by a modifier of the form (:...)
even if no filename
generation is performed.
Thus:
ls *(-/)
lists all directories and symbolic links that point to directories, and
ls *(%W)
lists all world-writable device files in the current directory, and
ls *(W,X)
lists all files in the current directory that are world-writable or world-executable, and
echo /tmp/foo*(u0^@:t)
outputs the basename of all root-owned files beginning with the string `foo' in `/tmp', ignoring symlinks, and
ls *.*~(lex|parse).[ch](^D^l1)
lists all files having a link count of one whose names contain a dot
(but not those starting with a dot, since GLOB_DOTS
is explicitly
switched off) except for `lex.c', `lex.h', `parse.c', and
`parse.h'.
Go to the previous, next section.