Open epiccurious opened 11 months ago
For example:
printf "%s \n" "Hello world"
Solution
Use printf "%b" whatever, or test for the system and set xpg_echo using shopt -s xpg_echo as needed.
If you omit the "%b" format string (for example, printf whatever), then printf will try to interpret any % characters in whatever, which is probably not what you want. The "%b" format is an addition to the standard printf format that will prevent that misinterpretation and also expand backslash escape sequences in whatever.
Setting xpg_echo is less consistent since it only works on bash. It can be effective if you are sure that you’ll only every run under bash, and not under sh or another similar shell that doesn’t use xpg_echo.
Using printf requires changes to how you write echo statements, but it’s defined by POSIX and should be consistent across any POSIX shell anywhere. Specifically, you have to write printf "%b" instead of just echo.
printf
is POSIX, whileecho -e
isn't.https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo