How can I have a newline in a string in sh?

sh
378,859

Solution 1

If you're using Bash, the solution is to use $'string', for example:

$ STR=$'Hello\nWorld'
$ echo "$STR" # quotes are required here!
Hello
World

If you're using pretty much any other shell, just insert the newline as-is in the string:

$ STR='Hello
> World'

Bash is pretty nice. It accepts more than just \n in the $'' string. Here is an excerpt from the Bash manual page:

Words of the form $'string' are treated specially. The word expands to
string, with backslash-escaped characters replaced as specified by the
ANSI C standard. Backslash escape sequences, if present, are decoded
as follows:
      \a     alert (bell)
      \b     backspace
      \e
      \E     an escape character
      \f     form feed
      \n     new line
      \r     carriage return
      \t     horizontal tab
      \v     vertical tab
      \\     backslash
      \'     single quote
      \"     double quote
      \nnn   the eight-bit character whose value is the octal value
             nnn (one to three digits)
      \xHH   the eight-bit character whose value is the hexadecimal
             value HH (one or two hex digits)
      \cx    a control-x character
The expanded result is single-quoted, as if the dollar sign had not
been present.
A double-quoted string preceded by a dollar sign ($"string") will cause
the string to be translated according to the current locale. If the
current locale is C or POSIX, the dollar sign is ignored. If the
string is translated and replaced, the replacement is double-quoted.

Solution 2

Echo is so nineties and so fraught with perils that its use should result in core dumps no less than 4GB. Seriously, echo's problems were the reason why the Unix Standardization process finally invented the printf utility, doing away with all the problems.

So to get a newline in a string, there are two ways:

# 1) Literal newline in an assignment.
FOO="hello
world"
# 2) Command substitution.
BAR=$(printf "hello\nworld\n") # Alternative; note: final newline is deleted
printf '<%s>\n' "$FOO"
printf '<%s>\n' "$BAR"

There! No SYSV vs BSD echo madness, everything gets neatly printed and fully portable support for C escape sequences. Everybody please use printf now for all your output needs and never look back.

Solution 3

What I did based on the other answers was

NEWLINE=$'\n'
my_var="__between eggs and bacon__"
echo "spam${NEWLINE}eggs${my_var}bacon${NEWLINE}knight"
# which outputs:
spam
eggs__between eggs and bacon__bacon
knight

Solution 4

I find the -e flag elegant and straight forward

bash$ STR="Hello\nWorld"
bash$ echo -e $STR
Hello
World

If the string is the output of another command, I just use quotes

indexes_diff=$(git diff index.yaml)
echo "$indexes_diff"

Solution 5

The problem isn't with the shell. The problem is actually with the echo command itself, and the lack of double quotes around the variable interpolation. You can try using echo -e but that isn't supported on all platforms, and one of the reasons printf is now recommended for portability.

You can also try and insert the newline directly into your shell script (if a script is what you're writing) so it looks like...

#!/bin/sh
echo "Hello
World"
#EOF

or equivalently

#!/bin/sh
string="Hello
World"
echo "$string"  # note double quotes!
Share:
378,859
Juan A. Navarro
Author by

Juan A. Navarro

Academic background in computer science, currently working in software analysis/testing in industry. I'm a very enthusiastic TeX/LaTeX user, most days. I also run a small site for LaTeX beginners in spanish: LaTeX Fácil.

Updated on July 08, 2022

Comments

  • Juan A. Navarro
    Juan A. Navarro 5 months

    This

    STR="Hello\nWorld"
    echo $STR
    

    produces as output

    Hello\nWorld
    

    instead of

    Hello
    World
    

    What should I do to have a newline in a string?

    Note: This question is not about echo. I'm aware of echo -e, but I'm looking for a solution that allows passing a string (which includes a newline) as an argument to other commands that do not have a similar option to interpret \n's as newlines.

  • Juan A. Navarro
    Juan A. Navarro over 10 years
    Thanks for this answer, I think it provides really good advice. Note, however, that my original question wasn't really about how to get echo to print newlines, but how to get a newline into a shell variable. You show how to do that using printf as well, but I think that the solution from amphetamachine using $'string' is even cleaner.
  • Oliver Turner over 10 years
    That's valid bash, but not POSIX sh.
  • Jens
    Jens almost 10 years
    Only for a deficient definition of 'clean'. The $'foo' syntax is not valid POSIX syntax as of 2013. Many shells will complain.
  • totowtwo almost 10 years
    Thanks for this answer. I get so screwed trying to use bash specific stuff in sh files all too often.
  • Yordan Georgiev
    Yordan Georgiev over 9 years
    +1-just a note: export varDynamic="$varAnother1$varAnother2"$'\n'
  • Jonny
    Jonny over 9 years
    BAR=$(printf "hello\nworld\n") does not print the trailing \n for me
  • Jens
    Jens over 9 years
    @Jonny It shouldn't; the shell specification for command substitution says that one or more newlines at the end of the output are deleted. I realize this is unexpected for my example and have edited it to include a newline in the printf.
  • ishmael
    ishmael over 8 years
    @Jens You show how to print a trailing newline after a variable, but how does one preserve the trailing newline WITHIN the variable? That is to say, I'd like to be able to say: echo "$BAR" something and have 'something' show up on a new line.
  • Jens
    Jens over 8 years
    @ishmael Then you need to make the newline part of the variable value, like I do with FOO (remove world to leave only " on that line.)
  • ishmael
    ishmael over 8 years
    @Jens Thanks, but I was hoping for a way of doing it using '\n' and not a literal newline (which forces me to type two lines). Is there any way of making the $(...) evaluation retain the trailing newline?
  • Jens
    Jens over 8 years
    @ismael No, $() is specified by POSIX as removing sequences of one or more <newline> characters at the end of the substitution.
  • Ostap
    Ostap about 8 years
    on POSIX I would type IFS='^J' and use that. Any other option known?
  • Juan A. Navarro
    Juan A. Navarro over 7 years
    This has already been mentioned in other answers. Also, although it was already there, I've just edited the question to stress the fact that this question is not about echo.
  • Rohit Gupta
    Rohit Gupta over 7 years
    This is not an answer to the question
  • Admin
    Admin over 7 years
    Well (in bash), try printf -v var "%s\n\n" "valuea" "valueb". It seems to retain the last two \n. Clue: echo -n "$var"|xxd. :)
  • Admin
    Admin over 7 years
    In sh: var="$(printf "%s\n\n" "vala" "valb" "x")";echo -n "${var%x}" |xxd'
  • Admin
    Admin over 7 years
    That's: add an x at the end and then remove it. And redefine the printf to this new command. Redefinition and the x trick from Greg's Wiki point 41 of Bash Pitfails.
  • Kieveli
    Kieveli about 7 years
    This totally answers the question I was looking for! Thanks!
  • Mateus Leon over 6 years
    Worked well for me here. I've used to create a file based on this echo and it generated new lines on the output correctly.
  • Qian
    Qian over 6 years
    What are echo's problems really?
  • ssc
    ssc about 6 years
    This does indeed work with literal strings as the OP asked, but fails as soon as variables are to be substituted: STR=$"Hello\nWorld" simply prints Hello\nWorld.
  • miken32
    miken32 over 5 years
    @ssc single quotes, not double
  • Jens
    Jens over 5 years
    @TheoYou Echo's problems are that it is unspecified what it does when the first arg is -e, -n or -c and what happens when an arg ends in \c. BSD and SYSV echo behave differently. In a word: echo options are unportable, while printf is fully specified.
  • pyb
    pyb over 5 years
    Good old heredoc. And it's probably POSIX compliant too!
  • JDS
    JDS about 5 years
    @ssc there are no variables in your example. Also, this method requires single quotes. Finally, to include interpolated variables you would have to concatenate double-quoted and special-single-quoted strings together. e.g. H="Hello"; W="World"; STR="${H}"$'\n'"${W}"; echo "${STR}" will echo "Hello" and "World" on separate lines
  • ssc
    ssc almost 5 years
    @JDS (and miken32): Thanks for the extra explanation, much appreciated! Makes for ugly code, but works. :-)
  • tripleee
    tripleee almost 5 years
    @TheoYou Predictably, there is a separate question with a good answer to that. This is literally my first Google hit for "why is printf preferred over echo": stackoverflow.com/questions/11530203/…
  • dlamblin
    dlamblin over 4 years
    Thanks, now I can: less "$(printf '+&^ INFO\nF')" /var/log/some*.log
  • tripleee
    tripleee over 3 years
    That's a very tedious way to write str=$(printf '%s\n' "first line" "another line" "and another line") (the shell will conveniently (or not) trim any final newlines from the command substitution). Several answers here already promote printf in various forms so I'm not sure this adds any value as a separate answer.
  • Adam K Dean
    Adam K Dean over 3 years
    It is for short strings, but if you want to keep things tidy and each line is actually 60 characters long, then it's a tidy way of doing it. Given that it is the solution I eventually went with, it does add something, if not for you, then for myself in the future when I undoubtedly return.
  • tripleee
    tripleee over 3 years
    echo -e is notorious for its portability issues. This is less of a Wild West situation now than pre-POSIX, but there is still no reason to prefer echo -e. See also stackoverflow.com/questions/11530203/…
  • tripleee
    tripleee over 3 years
    Even for long strings I prefer printf '%s\n' "first line" \ (newline, indent) 'second line' etc. See also printf -v str for assigning to a variable without spawning a subshell.
  • Shadi
    Shadi over 3 years
    The quotes around $NEWSTR in echo "$NEWSTR" are important here
  • Dee
    Dee almost 3 years
    how to have \n in sh? (not bash)
  • Peter Mortensen
    Peter Mortensen 9 months
    Since this is more or less the canonical question for the non-Bash question (and this is chronologically the first answer) and most importantly that the search engines like it, perhaps add an example for printf? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)