On Mon, Jul 26, 2010 at 04:27:30PM -0500, Peng Yu wrote: > Could you > please let me know when to use printf instead of echo?
There is never any need to use echo. printf does everything echo does, and does it better -- but people are comfortable with echo, because it's simple, and they've been trained by 15 years of legacy scripts that used it. > In particular, I want to print a string that has single quote, double > quote and dollar character. printf "%s\n" "$mystring" It makes no difference what the contents of the variable are. > I could use " to enclose such string and > add '\' to escape " and $ in the string. Oh, you mean to express a string constant in the code? That has nothing at all to do with the commands you give it to. mystring='whatever'$'whatever'"whatever"\w\h\a\t\e\v\e\r Do whatever it takes. Concatenate multiple quoting styles as needed. Back to your specific complaint: > In particular, I want to print a string that has single quote, double > quote and dollar character. Meaning "I want to create a string constant that contains a single quote, a double quote and a dollar sign." Style 1: \'\"\$ Style 2: \''"$' Style 3: "'\"\$" Style 4: $'\'"$' etc. At this point it's a matter of personal choice. Pick whichever style looks least ugly to you. There are an infinite number of ways you could write this (if you discard the ways that simply append '' and the like to the constant for no reason, it becomes finite, but I have no intention of counting them all).