- How can I have a newline in a string in sh?
- 13 Answers 13
- Insert Newline in Shell Command
- 1. Introduction
- 2. Newline and Buffer Submission
- 3. Automatically Continue Command on a New Line
- 4. Insert Newline in a Command
- 4.1. Universal Newline
- 4.2. Zsh
- 5. Bash and Ksh Command Line Editing
- 6. Major Shells and Newline Insertion Techniques
- 7. Summary
- Insert newline (\n) using sed
- Adding newline characters to unix shell variables
- 9 Answers 9
How can I have a newline in a string in sh?
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.
13 Answers 13
If you’re using Bash, you can use backslash-escapes inside of a specially-quoted $’string’ . For example, adding \n :
STR=$'Hello\nWorld' echo "$STR" # quotes are required here!
If you’re using pretty much any other shell, just insert the newline as-is in the string:
Bash recognizes a number of other backslash escape sequences 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.
Insert Newline in Shell Command
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
1. Introduction
The shell is the main entry point for all but the most obscure embedded Linux distributions. Even in the latter, we should be able to run instructions in some form or another. Further, one of the most universal interactive ways to execute commands is to type them in with a keyboard.
In this tutorial, we discuss interactively-typed shell commands and how to insert a manual newline in the middle of a command. First, we go over the difference between adding a newline and submitting a command. After that, we talk about automatic newline insertion based on syntax. Next, we explore techniques to interactively include a newline within a shell command. Then, we look at command-line editing. Finally, we perform a comparison between different methods of command newline insertion based on the environment.
We tested the code in this tutorial on Debian 12 (Bookworm) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments unless otherwise specified.
2. Newline and Buffer Submission
Running a shell command usually involves two steps at the prompt:
Like other major operating systems, Linux uses a newline via the Return (Enter) key to trigger a line buffer submission.
$ printf 'Line 1.\nLine2.\n'[Return] Line 1. Line 2. $
In this case, we use printf for outputting two lines of text. To do so, we enter the command and press Return.
This special role of the newline might present challenges in the same way that filenames with newlines could. Also, sometimes we might want to have a newline as part of the buffer instead of its end and submission signal.
3. Automatically Continue Command on a New Line
Naturally, most major shells can detect basic syntax interactively. Thus, unfinished surrounding constructs start another line instead of submitting the current buffer when Return is pressed:
$ printf 'Line 1.[Return] > Line 2.[Return] > ' Line 1. Line 2. $
In this case, a > right angle bracket and a space precede each continuation line, but they aren’t part of the final buffer contents when submitting. The prefix might be different for shells other than Bash. Critically, we can’t go back to a previous line from a line starting with >.
The mechanism above is valid for other structures as well:
Notably, the automatic line continuation usually doesn’t work for (square) brackets, since they are either just the [ (test) built-in (Bash) or excluded from the line syntax handling.
In contrast, a semi-automatic way of continuing a command on the next line without preserving the newlines is to escape the newline (Return) character. We do this by inserting a backslash at the end of a line like in this echo example without quotes:
$ echo Line 1.\[Return] > Line 2.\[Return] > [Return] Line 1.Line 2.
In all cases above, we just visually format our command buffer but only preserve the newline characters when they’re within quotes.
Now, let’s see how to actually insert a newline by hand.
4. Insert Newline in a Command
In most shells, we can use caret notation to both insert and display special characters and control sequences.
Notably, as long as the shell supports it, all options for inserting newlines that we look at below allow us to go back through the lines via the Left arrow key.
4.1. Universal Newline
One of the available key combinations that are fairly universal involves the input of Ctrl+V followed by Ctrl+J:
$ echo Line 1.[Ctrl+V, Ctrl+J] Line 2.[Ctrl+V, Ctrl+J] Line 1. [. ]Line: command not found $
Critically, the shell still considers the newline character as a command submission. Because of this, echo only works with Line 1., but Line 2. is seen as its own command and results in an error.
In fact, sh and dash even insert ^J, while [t]csh inserts ^M, all with the same combination. Upon submission of the buffer, ^J and ^M do add a newline to the output.
In other words, we can use Ctrl+V, Ctrl+J to create a script interactively in most shells.
4.2. Zsh
There are two newline combinations specific to the Zsh shell that work just like Ctrl+V, Ctrl+J, which is also available in Zsh:
In most other shells, the combinations above produce and submit a ^[ control sequence inducer (CSI).
Still, we can get the same combinations to work in Bash by adding a line to $HOME/.inputrc:
After we modify $HOME/.inputrc, we can either restart the shell or use bind to load the current entries of the file:
The latter option doesn’t remove old bindings.
5. Bash and Ksh Command Line Editing
The Bash and Korn shells in particular offer another feature that enables arbitrary edits and insertion of any command:
By using the combination Ctrl+X, Ctrl+E, we tell Bash to perform two actions:
- save the current command line content in a temporary file
- open the temporary command file in the default text editor
Once ready with any edits, we save our changes and exit the editor. At this point, Bash or Korn runs the temporary file with our command as a shell script and removes it.
Of course, we can add any number of newlines and apply formatting.
6. Major Shells and Newline Insertion Techniques
Often, we only consider a number of shells as major ones in the Linux ecosystem:
- sh – classic Bourne Shell, rudimentary, POSIX-compliant, and default for UNIX v7
- [t]csh – C shell, fairly basic, C-like, with tcsh as its expansion
- bash – Bourne Again Shell, a considerable improvement over sh with many complex features
- dash – Debian Almquist Shell, simpler and fully POSIX-compliant, unlike bash, mainly meant to replace sh as a more feature-rich default
- zsh – Z Shell, feature-rich, and the default for the Kali Linux distribution
- ksh – Korn Shell, POSIX-compliant, yet also feature-rich
Thus, we can create a comparison table of the usual ways to insert a newline and what shell supports which methods:
+-----------------------------------------------------+ | | sh | csh | bash | dash | zsh | ksh | |----------------+----+-----+------+------+-----+-----| | Syntax detect | + | - | + | + | + | + | |----------------+----+-----+------+------+-----+-----| | Editor | - | - | + | - | - | + | |----------------+----+-----+------+------+-----+-----| | Ctrl+V, Ctrl+J | - | - | + | - | + | - | |----------------+----+-----+------+------+-----+-----| | Alt-Return | - | - | / | - | + | - | |----------------+----+-----+------+------+-----+-----| | Esc-Return | - | - | / | - | + | - | +-----------------------------------------------------+
Here, a / slash means it’s not supported out-of-the-box but can be added without much effort.
7. Summary
In this article, we discussed ways to insert a newline in a shell command.
In conclusion, there are different methods we can leverage to add newlines interactively within commands, and they mainly depend on the shell in use.
Insert newline (\n) using sed
I am trying to scrub some lists into a properly formatted CSV file for database import. My starting file, looks something like this with what is supposed to be each «line» spanning multiple lines like below
Mr. John Doe Exclusively Stuff, 186 Caravelle Drive, Ponte Vedra 33487.
I created a sed script that cleans up the file (there’s lots of «dirty» formatting like double spaces and spaces before/after commas). The problem is the Zip with the period. I would like to change that period for a new line, but I cannot get it to work. The command that I use is:
:a N s|[[:space:]][[:space:]]| |g s|,[[:space:]]|,|g s|[[:space:]],|,|g s|\n| |g s|[[:space:]](2)\.|,FL,\1\n |g $!ba
Mr. John Doe,Exclusively Stuff,186 Caravelle Drive,Ponte Vedra,FL,33487n
If figured that the Zip+.(period) would be a great «delimiter» to use the substitution on and while I can find it, I can’t seem to tell it to put a newline there. Most of the things I found online are about replacing the newline with something else (usually deleting them), but not much on replacing with a newline. I did find this, but it didn’t work: How to insert newline character after comma in `),(` with sed? Is there something I am missing? Update: I edited my scrub.sed file putting the literal new line as instucted. It still doesn’t work
:a N s|[[:space:]][[:space:]]| |g s|,[[:space:]]|,|g s|[[:space:]],|,|g s|\n| |g s|[[:space:]](2)\.|,FL,\1\ |g $!ba
Mr. John Doe,Exclusively Stuff,186 Caravelle Drive,Ponte Vedra,FL,33487 Mrs. Jane Smith,Props and Stuff,123 Main Drive,Jacksonville,FL,336907
Mr. John Doe,Exclusively Stuff,186 Caravelle Drive,Ponte Vedra,FL,33487 Mrs. Jane Smith,Props and Stuff,123 Main Drive,Jacksonville,FL,336907
Adding newline characters to unix shell variables
I have a variable in a shell script in which I’d like to format the data. The variable stores new data during every iteration of a loop. Each time the new data is stored, I’d like to insert a new line character. Here is how I’m trying to store the data into the variable. VARIABLE=»$VARIABLE ‘\n’ SomeData» Unfortunately, the output includes the literal ‘\n’ Any help would be appreciative.
9 Answers 9
Thanks vmpstr. I may be mistaken, but I believe VAR=$VAR$’\n’b must be surrounded by double quotes. Which results in a literal ‘\n’.
You can switch between different kinds of quotes in the same «word»: VAR=»$VAR»$’\n'»some data» has $VAR in double-quotes, \n in $» so it’ll get interpreted as a newline, and then some data back in double-quotes. As long as there’s no space between them, they’ll all be concatenated together and the result assigned to VAR.
The output of echo is different than how the values are being stored in VARIABLE. The contents of VARIABLE are passed to an email util. I am not echoing the data to command line. Sorry for the confusion.
I got it! Turns out I had some spacing issues. Thanks to everyone who cared to comment, especially Gordon Davisson. Here was my issue: Original Code: VAR=»$VAR»$’\n'»Audit Source: $B Audit Path: $C» The spaces for formatting was messing things up. New Code: VAR=»$VAR»$’\n'»Audit Source:$B»$’\t'»Audit Path:$C» No spaces here! Anyone know why the spacing causes issues? Even though the Original Code has everything surrounded by «?