Bash Is Not Zsh - Whitespace in variables
If you think bash and zsh are alike, ask someone who has had to spend hours trying to get a shell script to work after switching between shells.
One difference that many shell scripters encounter is the bash behavior of splitting a string with whitespaces when used with a for-in loop. The behavior makes it easy to quickly write scripts with minimal repetitions. For example:
ULIMIT_PARAMS="cpu as memlock fsize"
for p in $ULIMIT_PARAMS
do
echo "* soft $p unlimited" >> /etc/security/limits.conf
echo "* hard $p unlimited" >> /etc/security/limits.conf
done
Bash will separate the string at whitespaces, attempt to evaluate wildcards, and iterate through the elements: cpu, as, memlock, and fsize. The output will therefore be the addition of the following lines in /etc/security/limits.conf:
* soft cpu unlimited
* hard cpu unlimited
* soft as unlimited
* hard as unlimited
* soft memlock unlimited
* hard memlock unlimited
* soft fsize unlimited
* hard fsize unlimited
However, zsh would just see one long string “cpu as memlock fsize”, and this would be the output:
* soft cpu as memlock fsize unlimited
* hard cpu as memlock fsize unlimited
To get the above example to work with zsh, we can run the string through an echo command like this:
ULIMIT_PARAMS="cpu as memlock fsize"
for p in $(echo $ULIMIT_PARAMS)
do
echo "* soft $p unlimited" >> /etc/security/limits.conf
echo "* hard $p unlimited" >> /etc/security/limits.conf
done
With the added echo, the output will now be identical to what we see in bash.