Different behaviour with echo vs printf command line arguments in zsh

1.1k Views Asked by At

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$ 
1

There are 1 best solutions below

0
On

echo and printf (and print) are different commands with different syntaxes and behaviors. Read the documentation for more info:

In Zsh, you should always prefer print or printf over echo. echo is mostly just there for compatibility with other shells.