POSIX-compliant shells


Table of Contents

IEEE Std 1003 
2.6.2 Parameter Expansion 
2.6.3 Command Substitution 
2.6.4 Arithmetic Expansion 
array in POSIX-compliant shells 
wc -l output type 
Rename 
Mass re-name files 

IEEE Std 1003 

http://www.opengroup.org/onlinepubs/009695399/toc.htm IEEE Std 1003.1, 2004 Edition

Shell Command Language http://www.opengroup.org/onlinepubs/009695399/idx/shell.html

Download a Personal-Use Copy of the Standard http://www.opengroup.org/onlinepubs/009695399/download/ The files were last updated Fri Jan 21 10:30:24 GMT 2005

documented on: 2005.05.22

2.6.2 Parameter Expansion 

http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06

${parameter:-word}

Use Default Values. If parameter is unset or null, the expansion of word shall be substituted; otherwise, the value of parameter shall be substituted.

${parameter:=word}

Assign Default Values. If parameter is unset or null, the expansion of word shall be assigned to parameter. In all cases, the final value of parameter shall be substituted. Only variables, not positional parameters or special parameters, can be assigned in this way.

${parameter:?[word]}

Indicate Error if Null or Unset. If parameter is unset or null, the expansion of word (or a message indicating it is unset if word is omitted) shall be written to standard error and the shell exits with a non-zero exit status. Otherwise, the value of parameter shall be substituted. An interactive shell need not exit.

${parameter:+word}

Use Alternative Value. If parameter is unset or null, null shall be substituted; otherwise, the expansion of word shall be substituted.

In the parameter expansions shown previously, use of the colon in the format shall result in a test for a parameter that is unset or null; omission of the colon shall result in a test for a parameter that is only unset. The following table summarizes the effect of the colon:

${#parameter}

String Length. The length in characters of the value of parameter shall be substituted. If parameter is '*' or '@', the result of the expansion is unspecified.

The following four varieties of parameter expansion provide for substring processing. In each case, pattern matching notation (see Pattern Matching Notation), rather than regular expression notation, shall be used to evaluate the patterns.

If parameter is '*' or '@', the result of the expansion is unspecified. Enclosing the full parameter expansion string in double-quotes shall not cause the following four varieties of pattern characters to be quoted, whereas quoting characters within the braces shall have this effect.

${parameter%word}

Remove Smallest Suffix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the suffix matched by the pattern deleted.

${parameter%%word}

Remove Largest Suffix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest portion of the suffix matched by the pattern deleted.

${parameter#word}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted.

${parameter##word}

Remove Largest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest portion of the prefix matched by the pattern deleted.

Examples 

parameter:-word 
${parameter:-word}

In this example, ls is executed only if x is null or unset. (The $( ls) command substitution notation is explained in Command Substitution.)

${x:-$(ls)}
parameter:=word 
${parameter:=word}
unset X
echo ${X:=abc}
abc
echo ${X}
abc
parameter:?word 
${parameter:?word}
unset posix
echo ${posix:?}
sh: posix: parameter null or not set
parameter:+word 
${parameter:+word}
set a b c
echo ${3:+posix}
posix
#parameter 
${#parameter}
HOME=/usr/posix
echo ${#HOME}
10
parameter%word 
${parameter%word}
x=file.c
echo ${x%.c}.o
file.o
parameter%%word 
${parameter%%word}
x=posix/src/std
echo ${x%%/*}
posix
parameter#word 
${parameter#word}
x=$HOME/src/cmd
echo ${x#$HOME}
/src/cmd
parameter##word 
${parameter##word}
x=/one/two/three
echo ${x##*/}
three

The double-quoting of patterns is different depending on where the double-quotes are placed:

"${x#*}"
   The asterisk is a pattern character.
${x#"*"}
   The literal asterisk is quoted and not special.

2.6.3 Command Substitution 

Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution shall occur when the command is enclosed as follows:

$(command)

2.6.4 Arithmetic Expansion 

Arithmetic expansion provides a mechanism for evaluating an arithmetic expression and substituting its value. The format for arithmetic expansion shall be as follows:

$((expression))

Examples 

A simple example using arithmetic expansion:

# repeat a command 100 times
x=100
while [ $x -gt 0 ]
do
  command
  x=$(($x-1))
done

More Samples 

 x=100

 $ echo $(($x-1))
 99

 $ echo $(( ($x-1) % 10 /3 << 2 ))
 12

 $ echo $(( ($x-1) % 10 /3 << 2 > 11 ))
 1

 $ echo $((x-=11))
 89

 $ echo $((x))
 89

 $ echo $(($x-=11))
 bash: 100-=11: attempted assignment to non-variable (error token is "-=11")

documented on: 2005.05.22