I want to print $Usage using printf into a bash script.
Usage="Usage: \n \
\tjkl [-lbmcdxh] [-f filename]\n \
\t\t[-h] \tThis usage text.\n \
\t\t[-f filename] \t cat the specified file. \n \
\t\t[-l] \tGo to application log directory with ls. \n \
\t\t[-b] \tGo to application bin directory. \n \
\t\t[-c] \tGo to application config directory.\n \
\t\t[-m] \tGo to application log directory and more log file.\n \
\t\t[-d] \tTurn on debug information.\n \
\t\t[-x] \tTurn off debug information.\n"
How can I print it using printf?
I was thinking about using this:
printf "%s\n" $Usage
But it doesn't work.
The key point here is the lack of double quotes. Once you add them, you are done! This is because the quotes enable the expansion.
$ printf "$Usage"
Usage:
\
jkl [-lbmcdxh] [-f filename]
\
[-h] This usage text.
\
[-f filename] cat the specified file.
\
[-l] Go to application log directory with ls.
\
[-b] Go to application bin directory.
\
[-c] Go to application config directory.
\
[-m] Go to application log directory and more log file.
\
[-d] Turn on debug information.
\
[-x] Turn off debug information.
echo together with -e enables interpretation of backslash escapes, so this would also work:
echo -e "$Usage"
See the difference of quoting or not quoting in a simpler case:
$ printf "hello \nman\n"
hello
man
$
$ printf hello \nman\n
hello$
Finally, you may want to have a good read to: Why is printf better than echo?.
You can use:
printf "$Usage"
or
printf "%b" "$Usage"
From man 1 printf:
%b ARGUMENT as a string with `\' escapes interpreted, except that octal escapes are of the form \0 or \0NNN
If using %b don't forget to add double quotes around the argument
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With