How do I add a newline using printf?

How do I add a new line to a print command using printf?

printf "I want this on a new line!"

I thought it would be something like this but it didn't work

printf "/n I want this on a new line!/n"

Thanks in advance for the help!

2 Answers

To write a newline use \n not /n the latter is just a slash and a n

2

Try this:

printf '\n%s\n' 'I want this on a new line!'

That allows you to separate the formatting from the actual text. You can use multiple placeholders and multiple arguments.

quantity=38; price=142.15; description='advanced widget'
$ printf '%8d%10.2f %s\n' "$quantity" "$price" "$description" 38 142.15 advanced widget
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like