Parameter Expansion

Apart from this page, we do now have pageset that discusses parameter expansion in more detail. Please refer to Scripting:Parameter Expansion.

Substitutions tips

1. Use a variable name as reference to other variable
1.1 Numeric content

var1=5
tmp=var1
echo $((tmp))

1.2 All content

var=toto
tmp=var
echo ${(P)tmp}

Remove spaces from file names in the current directory (I replace them with underscores)

for file in *; do
  mv $file ${file:gs/\ /_/}
done

previous can also be done with zmv

zmv '(*)' '${1//\ /_/}'

Change filenames in current directory to lowercase

for file in *; do
  mv $file ${file//(#m)[A-Z]/${(L)MATCH}}
done

previous using zmv

zmv '(*)' '${1:l}'
 
examples/expnparam.txt · Last modified: 2010/01/05 09:20 (external edit)