Bash Parameter Expansion

user-pic
Vote 0 Votes

${parameter:-word} Use Default Values.
e.g.
echo ${A:-1}
A=2
echo ${A:-1}
result is
1
2

${parameter:=word} Assign Default Values.
e.g.
echo ${A:=1}
echo $A
A=2
echo ${A:=1}
result is
1
1
2

${parameter:?word} Display Error if Null or Unset.
equals [ -z $parameter ] && echo word && exit 1

${parameter:+word} Use Alternate Value.
equals [ -z $parameter ] || parameter=word
e.g.
echo ${A:+1}
A=2
echo ${A:+1}
result is

1

${parameter:offset}
${parameter:offset:length}
e.g.
A=string
echo ${A:1}
echo ${A:2:3}
result is
tring
rin

${!prefix*} Expands to the names of variables whose names begin with prefix
e.g.
A=test1
A1=test2
A2=test3
B=test4
echo ${!A*}
result is
A A1 A2

${#parameter} The length in characters of the value of parameter
e.g.
A=string
echo ${#A}
result is
6

${parameter#word} Head cut
${parameter##word}
e.g.
A=file.tar.gz
echo ${A#*.}
echo ${A##*.}
result is
tar.gz
gz

${parameter%word} Tail cut
${parameter%%word}
e.g.
A=file.tar.gz
echo ${A%.*}
echo ${A%%.*}
result is
file.tar
file

${parameter/pattern/string} Pattern replacing
${parameter//pattern/string}
e.g.
A=linux_linux
echo ${A/linux/freebsd}
echo ${A//linux/freebsd}
result is
freebsd_linux
freebsd_freebsd

ref. man bash, / Parameter Expansion

${parameter:-word}
Use Default Values. If parameter is unset or null, the expan-
sion of word is substituted. Otherwise, the value of parameter
is substituted.
${parameter:=word}
Assign Default Values. If parameter is unset or null, the
expansion of word is assigned to parameter. The value of param-
eter is then substituted. Positional parameters and special
parameters may not be assigned to in this way.
${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset,
the expansion of word (or a message to that effect if word is
not present) is written to the standard error and the shell, if
it is not interactive, exits. Otherwise, the value of parameter
is substituted.
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is
substituted, otherwise the expansion of word is substituted.
${parameter:offset}
${parameter:offset:length}
Substring Expansion. Expands to up to length characters of
parameter starting at the character specified by offset. If
length is omitted, expands to the substring of parameter start-
ing at the character specified by offset. length and offset are
arithmetic expressions (see ARITHMETIC EVALUATION below).
length must evaluate to a number greater than or equal to zero.
If offset evaluates to a number less than zero, the value is
used as an offset from the end of the value of parameter. If
parameter is @, the result is length positional parameters
beginning at offset. If parameter is an array name indexed by @
or *, the result is the length members of the array beginning
with ${parameter[offset]}. Substring indexing is zero-based
unless the positional parameters are used, in which case the
indexing starts at 1.

${!prefix*}
Expands to the names of variables whose names begin with prefix,
separated by the first character of the IFS special variable.

${#parameter}
The length in characters of the value of parameter is substi-
tuted. If parameter is * or @, the value substituted is the
number of positional parameters. If parameter is an array name
subscripted by * or @, the value substituted is the number of
elements in the array.

${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in pathname
expansion. If the pattern matches the beginning of the value of
parameter, then the result of the expansion is the expanded
value of parameter with the shortest matching pattern (the ``#''
case) or the longest matching pattern (the ``##'' case) deleted.
If parameter is @ or *, the pattern removal operation is applied
to each positional parameter in turn, and the expansion is the
resultant list. If parameter is an array variable subscripted
with @ or *, the pattern removal operation is applied to each
member of the array in turn, and the expansion is the resultant
list.

${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in pathname
expansion. If the pattern matches a trailing portion of the
expanded value of parameter, then the result of the expansion is
the expanded value of parameter with the shortest matching pat-
tern (the ``%'' case) or the longest matching pattern (the
``%%'' case) deleted. If parameter is @ or *, the pattern
removal operation is applied to each positional parameter in
turn, and the expansion is the resultant list. If parameter is
an array variable subscripted with @ or *, the pattern removal
operation is applied to each member of the array in turn, and
the expansion is the resultant list.

${parameter/pattern/string}
${parameter//pattern/string}
The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat-
tern against its value is replaced with string. In the first
form, only the first match is replaced. The second form causes
all matches of pattern to be replaced with string. If pattern
begins with #, it must match at the beginning of the expanded
value of parameter. If pattern begins with %, it must match at
the end of the expanded value of parameter. If string is null,
matches of pattern are deleted and the / following pattern may
be omitted. If parameter is @ or *, the substitution operation
is applied to each positional parameter in turn, and the expan-
sion is the resultant list. If parameter is an array variable
subscripted with @ or *, the substitution operation is applied
to each member of the array in turn, and the expansion is the
resultant list.

Leave a comment

About this Entry

This page contains a single entry by Pank published on December 15, 2004 11:53 PM.

Get Windows computer name by IP was the previous entry in this blog.

77 IP Info is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Monthly Archives