I have a simple program to print the command line arguments in zsh. However, I see different behaviour with printf vs echo. Can anyone explain?
#!/bin/zsh
echo "$# @ : "$@" "
printf "\n"
printf "$# * $* "
printf "\n"
printf "$# @ : "$@" \n"
Output:
batman$ ./args a b c d
4 @ : a b c d
4 * a b c d
4 @ : abatman$ //also gobbles up the newline!!
batman$
echo
andprintf
(andprint
) are different commands with different syntaxes and behaviors. Read the documentation for more info:echo
print
printf
In Zsh, you should always prefer
print
orprintf
overecho
.echo
is mostly just there for compatibility with other shells.