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

array in POSIX-compliant shells 

Newsgroups:  comp.unix.shell
Date:        Thu, 25 Aug 2005 21:57:14 -0400
> Does POSIX-compliant shells support array of any kind?

Only the positional parameters.

Arrays can be simulated with eval, but they lack the versatility of those built into bash and ksh.

> Where can I find such info?

http://www.opengroup.org/onlinepubs/009695399/utilities/contents.html

Chris F.A. Johnson

wc -l output type 

> How do I convert wc -l output to numaric value and compare it with
> numaric value.
var=$(wc -l < filename)
[ $var -gt 100 ] && man wc

Rename 

> I was trying to use 'mv AE*.sas OM*.sas', but it was not working.

Using a POSIX shell, you can do

for i in AE*.sas; do mv "$i" "OM${i#AE}"; done

Mass re-name files 

http://groups.google.com/groups?selm=asbq9v%24pfsil%241%40ID-136730.news.dfncis.de

Newsgroups: comp.os.linux.help, comp.os.linux.misc
Date: 2002-11-30 17:57:22 PST
> > 6.)  Mass re-name files:  The best for last :).  I'm not a programmer, but
> > I've been trying to figure out a string that will rename all the files &
> > directories in a directory.  Right now, files & directories are a mix of
> > cases - I'd like to have everything be lower-case. For example:
>
> $ for  f  in *  ; do  mv $f  $( echo  $f  |  tr   A-Z   a-z )
>   ; done
>
> That's all one line.
  1. Which will fail if any file names contain spaces (or other problem characters).

  2. Which will generate error messages if the name is already lower case (as in one of the examples).

  3. Which will cause problems if another file already exists with the name in lower case.

The first problem is easy to fix; just quote the file names:

mv "$f" "$( echo $f | tr A-Z a-z )"

The second isn't hard, but it's more than you would want in a single line:

newname=$( echo $f | tr A-Z a-z )
[ "$f" != "$newname" ] && mv "$f" "$( echo $f | tr A-Z a-z )"

The third requires a decision on what to do if a file with the new name already exists. Do you want to overwrite it? Rename it? Leave it as is?

[ "$f" != "$newname" ] &&
  if [ -e "$newname" ]
  then
     ## what goes here depends on the decision mentioned above.
  else
     mv "$f" "$( echo $f | tr A-Z a-z )"
  fi

Chris F.A. Johnson