S in STCG
S in STCG

Reputation: 33

Bash: How to end printf or echo with a \ and then a new line

I would like to end printf or echo output with a backslash and move to a new line without having a space at the end of the first line.

For example when I have used the following:

printf "This is line one. \ \nThis is line two.

The output will have a space after the first \ , and then start the new line. I wondered how I can prevent or remove the end space.

Thanks for your help!

Upvotes: 3

Views: 872

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74605

You need more slashes!

printf 'This is line one. \\\nThis is line two.'

The first slash escapes the second one, then the third one is part of the newline \n.

Importantly, I've also changed the quotes to single. You could do it with double quotes but it'd look like this:

printf "This is line one. \\\\\nThis is line two."

The slashes used to escape other slashes need escaping themselves, which as I'm sure you will agree, is a mess!

Upvotes: 4

Related Questions