I wrote the following script when trying to understand how printf
works:
#!/usr/bin/bash
printf "Give me your three preferences.?
"
read p1
read p2
read p3
printf "%s\n" "${p1}" "${p2}" "${p3}"
printf "Just the same as this, \n"
printf "%s\n%s\n%s\n" "${p1}" "${p2}" "${p3}"
printf "I've found this way to be the easiest, ${p1}\n ${p2}\n ${p3}\n"
By trial-and-error, I figured out that the third way works - though it was not specified this learning resource. I use printf
in this way most frequently because it is echo
-like. Are there any cases or values of variables where such way of printing values to stdout
does not work? Why might I want to avoid such way of using printf
?
printf '%s\n' "$p1" "$p2" "$p3"
. See $VAR vs ${VAR} and to quote or not to quote – cas Aug 10 '23 at 10:03%60000s
chances it would segfault. – A.B Aug 10 '23 at 12:49