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

echo $((tmp))

1.2 All content

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/\ /_/}

previous can also be done with zmv

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

Removing character(s) from the beginning and/or end of a variable. The following code removes spaces.

# Removing character(s) from both the beginning and end
Variable="${${${Variable}%${Variable##*[^ ]}}#${Variable%%[^ ]*}}"
# Removing character(s) from end
Variable="${${Variable}%${Variable##*[^ ]}}"
# Removing character(s) from beginning
Variable="${${Variable}#${Variable%%[^ ]*}}"

Change filenames in current directory to lowercase

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

previous using zmv

zmv '(*)' '${1:l}'
examples/expnparam.txt · Last modified: 2016/09/26 03:41 by friartek