The types of expansions performed are
Exansion is done in the above specified order in five steps. The
first is history expansion, which is only performed in
interactive shells. The next step is alias expansion, which is
done right before the command line is parsed. They are followed by
process substitution, parameter expansion, command
substitution, arithmetic expansion and brace expansion
which are preformed in one step in left-to-right fashion. After
these expansions, all unquoted occurrences of the characters `\
',
`'
' and `"
' are removed, and the result is subjected to
filename expansion followed by filename generation.
If the SH_FILE_EXPANSION
option is set, the order of expansion is modified
for compatibility with sh and ksh. Filename expansion
is performed immediately after alias substitution,
preceding the set of five substitutions mentioned above.
Each word is checked to see if it begins with an unquoted `~
'.
If it does, then the word up to a `/
',
or the end of the word if there is no `/
',
is checked to see if it can be substituted in one of the ways
described here. If so, then the `~
' and the checked portion are
replaced with the appropriate substitute value.
A `~
' by itself is replaced by the value of $HOME
.
A `~
' followed by a `+
' or a `-
' is replaced by the value of
$PWD
or $OLDPWD
, respectively.
A `~
' followed by a number is replaced by the directory at that
position in the directory stack.
`~0
' is equivalent to `~+
',
and `~1
' is the top of the stack.
`~+
' followed by a number is replaced by the directory at that
position in the directory stack.
`~+0
' is equivalent to `~+
',
and `~+1
' is the top of the stack.
`~-
' followed by a number is replaced by the directory that
many positions from the bottom of the stack.
`~-0
' is the bottom of the stack.
The PUSHD_MINUS
option exchanges the effects of `~+
' and `~-
' where they are
followed by a number.
A `~
' followed by anything not already covered is looked up as a
named directory, and replaced by the value of that named directory if found.
Named directories are typically home 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 `/
'.
It is also possible to define directory names using the -d
option to the
hash
builtin.
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 shortest way of referring to the directory is used,
with ties broken in favour of using a named directory,
except when the directory is /
itself.
If a word begins with an unquoted `=
'
and the EQUALS
option is 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.
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 the PATH
parameter,
so that a `~
' or an `=
' following a `:
' is eligible for expansion.
All such behaviour can be
disabled by quoting the `~
', the `=
', or the whole expression (but not
simply the colon); the EQUALS
option is also respected.
.PP
If the option MAGIC_EQUAL_SUBST
is set, any unquoted shell
argument in 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 <
or >
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 on 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
cuts fields 1 and 3 from the files file1 and file2 respectively, pastes 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 (see man page 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 (see man page lseek(2))
on the input file.
The character `$
' is used to introduce parameter expansions.
See
section Parameters
for a description of parameters.
In the expansions discussed below that require a pattern, the form of
the pattern is the same as that used for filename generation;
see section Filename Generation. In addition to the following
operations, the file modifiers described in
section Modifiers in section History Expansion can be
applied: for example, ${i:s/foo/bar/}
performs string
substitution on the value of parameter $i
.
${
name}
${+
name}
1
' is substituted,
otherwise `0
' is substituted.
${
name:-
word}
${
name:=
word}
${
name::=
word}
${
name:?
word}
${
name:+
word}
${
name#
pattern}
${
name##
pattern}
(@)
flag or the `name[@]
' syntax
is used, matching is performed on each array elements separately.
${
name%
pattern}
${
name%%
pattern}
(@)
flag or the `name[@]
' syntax
is used, matching is performed on each array elements separately.
${
name:#
pattern}
(@)
flag or the `name[@]
' syntax
is used, matching is performed on each array elements separately, and
the matched array elements are removed (use the (M)
flag to
remove the non-matched elements).
${#
spec}
${^
spec}
RC_EXPAND_PARAM
option for the
evaluation of spec; if the `^
' is doubled, turn it off.
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'.
Internally, each such expansion is converted into the
equivalent list for brace expansion. E.g., ${^var}
becomes
{$var[1],$var[2],
...}
, and is processed as described in
section Brace Expansion below.
If word splitting is also in effect the
$var[
N]
may themselves be split into different list
elements.
${=
spec}
SH_WORD_SPLIT
option for the
evaluation of spec; if the `=
' is doubled, turn it off.
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; if the `~
' is doubled, turn it off. 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 a ${
...}
type parameter expression or a $(
...)
type command
substitution is used in place of name above, it is substituted first
and the result is used as it were the value of name.
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:
A
${
...:=
...}
or ${
...::=
...}
.
Assignment is made before sorting or padding.
@
"${(@)foo}"
' is equivalent to `"${foo[@]}"
' and
`"${(@)foo[1,2]}"
' is the same as `"$foo[1]" "$foo[2]"
'.
e
o
O
i
o
or O
, sort case-independently.
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.
W
w
with the difference that empty words between
repeated delimiters are also counted.
p
print
builtin
in string arguments to subsequent flags.
l:
expr::
string1::
string2:
r:
expr::
string1::
string2:
l
, but pad the words on the right.
j:
string:
SH_WORD_SPLIT
option.
F
pj:\n:
'.
s:
string:
SH_WORD_SPLIT
) at the
separator string. Splitting only occurs in places where an
array value is valid.
f
ps:\n:
'.
S
${
...#
...}
or
${
...%
...}
forms.)
Search substrings as well as beginnings or ends.
I:
expr:
M
R
B
E
N
A command enclosed in parentheses
preceded by a dollar sign, like `$(
...)
', or quoted with grave
accents, like ``
...`
', is replaced with its standard output, with any
trailing newlines deleted.
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]
' or
`$((
exp))
' is substituted
with the value of the arithmetic expression exp. exp is
subjected to parameter expansion, command substitution
and arithmetic expansion before it is evaluated.
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. Commas may be quoted in order to
include them literally in a word.
An expression of the form `{
n1..
n2}
',
where n1 and n2 are integers,
is expanded to every number between
n1 and n2 inclusive. If either number begins with a
zero, all the resulting numbers will be padded with leading zeroes to
that minimum width. If the numbers are in decreasing order the
resulting sequence will also be in decreasing order.
If a brace expression matches none of the above forms, it is left
unchanged, unless the BRACE_CCL
option is set.
In that case, it is expanded to a sorted list of the individual
characters between the braces, in the manner of a search set.
`-
' is treated specially as in a search set, but `^
' or `!
' as
the first character is treated normally.
If a word contains an unquoted instance of one of the characters
`*
', `(
', `|
', `<
', `[
', or `?
', it is regarded
as a pattern for filename generation, unless the GLOB
option is unset.
If the EXTENDED_GLOB
option is set,
the `^
' and `#
' characters also denote a pattern; otherwise
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 NOMATCH
option is unset, 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.
*
?
[
...]
-
'.
A `-
' or `]
' may be matched by including it as the
first character in the list.
[^
...]
[!
...]
[
...]
, except that it matches any character which is
not in the given set.
<
[x]-
[y]>
<->
' matches any number.
(
...)
KSH_GLOB
option is set, then a
`@
', `*
', `+
', `?
' or `!
' immediately preceding
the `(
' is treated specially, as detailed below.
|
y
|
' character
must be within parentheses, to avoid interpretation as a pipeline.
^
x
EXTENDED_GLOB
to be set.)
Matches anything except the pattern x.
This has a higher precedence than `/
', so `^foo/bar
'
will search directories in `.
' except `./foo
'
for a file named `bar
'.
~
y
EXTENDED_GLOB
to be set.)
Match anything that matches the pattern x but does not match y.
This has lower precedence than any operator except `|
', so
`*/*~foo/bar
' will search for all files in all directories in `.
'
and then exclude `foo/bar
' if there was such a match.
It groups left-to-right, so multiple patterns can be excluded by
`foo~
bar~
baz'.
In the exclusion pattern (y), `/
' and `.
' are not treated
specially the way they usually are in globbing.
#
EXTENDED_GLOB
to be set.)
Matches zero or more occurrences of the pattern x.
This operator has high precedence; `12#
' is equivalent to `1(2#)
',
rather than `(12)#
'.
##
EXTENDED_GLOB
to be set.)
Matches one or more occurrences of the pattern x.
This operator has high precedence; `12##
' is equivalent to `1(2##)
',
rather than `(12)##
'.
If the KSH_GLOB
option is set, the effects of parentheses can be
modified by a preceding `@
', `*
', `+
', `?
' or `!
'.
This character need not be unquoted to have special effects,
but the `(
' must be.
@(
...)
(
...)
'.)
*(
...)
(
...)#
'.)
+(
...)
(
...)##
'.)
?(
...)
(|
...)
'.)
!(
...)
(^(
...))
'.)
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
', not following
symbolic links. To follow links, use `***/
'.
Patterns used for filename generation may 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.
If the option BARE_GLOB_QUAL
is set, then a trailing set of parentheses
containing no `|
' or `(
' characters (or `~
' if it is special)
is taken as a set of
glob qualifiers. A glob subexpression that would normally be taken as glob
qualifiers, for example `(^x)
', can be forced to be treated as part of
the glob pattern by doubling the parentheses, for example `((^x))
'.
A qualifier may be any one of the following:
/
.
@
=
p
*
%
%b
%c
r
w
x
A
I
E
R
W
X
s
S
t
d
dev
l
[-
|+
]ct
-
), greater than
ct (+
), or is equal to ct
U
G
u
id
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 the user ID of this user will
be taken (e.g. `u:foo:
' or `u[foo]
' for user `foo
')
g
id
u
id but with group IDs or names
a
[Mwhm
][-
|+
]n
-
n). Files accessed more than n days ago are selected by a
positive n value (+
n). Optional unit specifiers `M
',
`w
', `h
' or `m
' (e.g. `ah5
') cause the check to be
performed with months (of 30 days), weeks, hours, or minutes instead of
days, respectively. For instance, `echo *(ah-5)
' would echo files
accessed within the last five hours.
m
[Mwhm
][-
|+
]n
c
[Mwhm
][-
|+
]n
L
[+
|-
]n
-
), more than n bytes (+
), or
exactly n bytes in length. If this flag is directly followed by a `k
'
(`K
'), `m
' (`M
'), or `p
' (`P
') (e.g. `Lk-50
')
the check is performed with kilobytes, megabytes, or blocks (of 512 bytes)
instead.
^
-
M
MARK_DIRS
option for the current pattern
T
LIST_TYPES
option, 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).
If a `:
' appears in a qualifier list, the remainder of the expression in
parenthesis is interpreted as a modifier (see section Modifiers
in section History Expansion). 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 actual 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
.
History substitution allows you to use words from previous command
lines in the command line you are typing. This simplifies spelling
corrections and the repetition of complicated commands or arguments.
Command lines are saved in the history list, the size of which
is controlled by the HISTSIZE
variable. The most recent command is retained in any case.
A history substitution begins with the first character of the
histchars
parameter which is `!
'
by default and may occur anywhere on the command line; history
substitutions do not nest. The `!
' can be escaped with `\
'
or can be enclosed between a pair of single quotes (''
) to suppress
its special meaning. Double quotes will not work for this.
Input lines containing history substitutions are echoed on the terminal after being expanded, but before any other substitutions take place or the command gets executed.
An event designator is a reference to a command-line entry in the history list.
!
=
' or `(
'.
!!
!
n
!-
n
!
str
!?
str[?
]
!#
!#
' reference.
!{
...}
A word designator indicates which word or words of a given command line will
be included in a history reference. A `:
'
separates the event specification from the word designator.
It can be omitted if the word designator begins with a
`^
', `$
', `*
', `-
' or `%
'.
Word designators include:
0
^
1
.
$
%
?
str search.
-
y
0
.
*
*
-$
'.
-
*
' but omitting word $
.
Note that a `%
' word designator will only work when used as
`!%
', `!:%
' or `!?
str?:%
',
and only when used after a !?
substitution. Anything else will result
in an error, although the error may not be the most obvious one.
After the optional word designator, you can add
a sequence of one or more of the following modifiers,
each preceded by a `:
'. These modifiers also work on the result
of filename generation and parameter expansion, except where
noted.
h
r
.
xxx', leaving the basename.
e
t
p
q
x
q
, but break into words at each blank.
l
u
f
F
, w
and W
modifier only work with parameter and
filename expansion.)
Repeats the immediately (without a colon) following modifier until the
resulting word doesn't change any more.
F:
expr:
f
, but repeats only n times if the expression
expr evaluates to n. Any character can be used instead of
the `:
', if any of `(
', `[
', or `{
'
is used as the opening delimiter
the second one has to be ')
', `]
', or `}
' respectively.
w
W:
sep:
w
but words are considered to be the parts of the string
that are separated by sep. Any character can be used instead of
the `:
'; opening parentheses are handled specially, see above.
s/
l/
r[/
]
g
, with no colon between,
the substitution is done only for the
first string that matches l. For arrays and filename
substitution, this applies to each word of the expanded text.
&
s
substitution. Like s
, may be preceded
immediately by a g
. In variable expansion the &
must appear
inside braces, and in filename expansion it must be quoted with a
backslash.
The s/l/r/
substitution works as follows. The left-hand side of
substitutions are not regular expressions, but character strings. Any
character can be used as the delimiter in place of `/
'. A
backslash quotes the delimiter character. The character `&
', in
the right hand side, is replaced by the text from the left-hand-side.
The `&
' can be quoted with a backslash. A null l uses the
previous string either from a l or from a contextual scan string
s from `!?
s'. You can omit the rightmost delimiter if a
newline immediately follows r; the rightmost `?
' in a context
scan can similarly be omitted. Note the same record of the last
l and r is maintained across all forms of expansion.
By default, a history reference with no event specification refers to the same
line as the last history reference on that command line, unless it is the
first history reference in a command. In that case, a history reference
with no event specification always refers to the previous command. However,
if the option CSH_JUNKIE_HISTORY
is set,
then history reference with no
event specification will always refer to the previous command.
For example, `!!:1
'
will always refer to the first word of the previous command, and `!!$
'
will always refer to the last word of the previous command. And with
CSH_JUNKIE_HISTORY
set, then `!:1
' and `!$
'
will function in the same manner as `!!:1
' and `!!$
',
respectively. However, if CSH_JUNKIE_HISTORY
is unset, then
`!:1
' and `!$
'
will refer to the first and last words respectively, of the last command
referenced on the current command line. However, if they are the first history
reference on the command line, then they refer to the previous command.
The character sequence `^
foo^
bar'
repeats the last command, replacing the string foo with bar.
If the shell encounters the character sequence `!"
'
in the input, the history mechanism is temporarily disabled until
the current list is fully parsed. The `!"
'
is removed from the input, and any subsequent `!
'
characters have no special significance.
A less convenient but more comprehensible
form of command history support
is provided by the fc
builtin.
Go to the first, previous, next, last section, table of contents.