Zsh do not auto split string into words like bash.
1
2
3
4
5
6
7
8
9
10
11
12
13
STR="line 1
line2"# The :: is printed once only.for i in ${STR}; do echo :: ${i}; done:: line 1line2
# Add = in front of variable to split it like bash.for i in ${=STR}; do echo :: ${i}; done:: line
:: 1:: line2
Compare [[ (statement) ]]
[[ ${a} = ${b} ]].
Space after [[ and before ]].
Space on both sides of operator =, <, >.
1
2
3
4
5
6
7
8
9
10
a=1; b=2if [[${a} = ${b}]] ... # zsh: no matches found: [[1=2]]if [[${a}=${b}]] ... # zsh: bad pattern: [[1if [[ ${a}=${b} ]] ... # This will always be true. Logical error.if [[ ${a} = ${b} ]] # Correctthen ...
fi
Evaluate / Math (( ))
1
2
3
4
A=2; C=1((C+=1)) # C = 2((A=A+C)) # A = 4echo A:${A} C:${C}
For loop number
{${i}..${j}}
NO SPACE after { and before }
1
2
for i in {1..10}; do ...; donefor i in {1..${VAR}}; do ...; done
Associative Array
typeset -A VAR_NAME
Array index can be number or string
Variable can be used in index and will be evaluated.
a=10b="abc"# This is ok, not inside index.typeset -A MY_ARRAY
MY_ARRAY[1]=1MY_ARRAY[${a}]=1# Index is 10, MY_ARRAY[10]=1MY_ARRAY[2,${b}]="Hello"# Index is [2,abc], MY_ARRAY[2,abc]="Hello!"