How to print a string using printf?

I want to print a string say "str1 str2 str3 str4" using printf.
If I try printing it using printf it is printing as follows.

output
-------
str1
str2
str3
str4

btw I'm working in AIX.

This is my first post in this forum :slight_smile:

regards,
rakesh

If it's in the Bash shell -

$
$ STR="str1 str2 str3 str4"
$
$ printf "%s\n" "$STR"
str1 str2 str3 str4
$
$

tyler_durden

... Don't know if AIX has Bash shell.

or

# printf "$STR\n"
str1 str2 str3 str4

It's best not to use printf that way, since character sequences that are special to printf (format specifiers, escape sequences) will be misinterpreted and lead to mangled output. To print the value of a variable, it's best to use:

printf '%s\n' "$STR"

Example of how things would go wrong:

$ STR='%s'
$ # Incorrectly prints nothing but a newline
$ printf "$STR\n"

$ # Correctly prints out the value of $STR
$ printf "%s\n" "$STR"
%s

Using the printf(1) utility, this is nothing but a minor bug. Using the printf(3) function in the C standard library, this would be a HUGE security hole.

Regards,
Alister

thanks guys the method suggested by durden_tyler and alister works.
@durden_tyler: I'm working in ksh :slight_smile: not bash.