3 ms·
It's well worth learning to use printf instead of echo to avoid all the various ways that echo can break or is not well defined. https://mywiki.wooledge.org/Ba
by ndsipa_pomu 4y ago
It's well worth learning to use printf instead of echo to avoid all the various ways that echo can break or is not well defined.
https://mywiki.wooledge.org/BashPitfalls#echo_.24foo https://mywiki.wooledge.org/BashPitfalls#echo_.24foo
- koolba 4y agoThe %q format alone justifies learning about printf. Vanilla echo is for chumps.
- hddqsb 4y agoNote that this is supported by bash/zsh/ksh but not by POSIX sh.
- hddqsb 4y agoYes. printf is not a "flex", it's the only safe way to display arbitrary strings. The link mentions one good reason to avoid echo (string might be "-n"), but another important reason is that the behaviour of echo is implementation-defined if the string contains backslashes (e.g. `echo 'foo\nbar'` displays one line in bash but two lines in dash and zsh).