How to insert a new line in Linux shell script?
The simplest way to insert a new line between echo
statements is to insert an echo
without arguments, for example:
echo Create the snapshots
echo
echo Snapshot created
That is, echo
without any arguments will print a blank line.
Another alternative to use a single echo
statement with the -e
flag and embedded newline characters \n
:
echo -e "Create the snapshots\n\nSnapshot created"
However, this is not portable, as the -e
flag doesn't work consistently in all systems. A better way if you really want to do this is using printf
:
printf "Create the snapshots\n\nSnapshot created\n"
This works more reliably in many systems, though it's not POSIX compliant. Notice that you must manually add a \n
at the end, as printf
doesn't append a newline automatically as echo
does.
How can I have a newline in a string in sh?
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!
Prints:
Hello
World
If you're using pretty much any other shell, just insert the newline as-is in the string:
STR='Hello
World'
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.
How to insert a newline in front of a pattern?
Some of the other answers didn't work for my version of sed.
Switching the position of &
and \n
did work.
sed 's/regexp/\n&/g'
Edit: This doesn't seem to work on OS X, unless you install gnu-sed
.
Insert newline (\n) using sed
The sed
on BSD does not support the \n
representation of a new line (turning it into a literal n
):
$ echo "123." | sed -E 's/([[:digit:]]*)\./\1\n next line/'
123n next line
GNU sed
does support the \n
representation:
$ echo "123." | gsed -E 's/([[:digit:]]*)\./\1\nnext line/'
123
next line
Alternatives are:
Use a single character delimiter that you then use tr
translate into a new line:
$ echo "123." | sed -E 's/([[:digit:]]*)\./\1|next line/' | tr '|' '\n'
123
next line
Or use an escaped literal new line in your sed script:
$ echo "123." | sed -E 's/([[:digit:]]*)\./\1\
next line/'
123
next line
Or define a new line:
POSIX:
nl='
'
BASH / zsh / others that support ANSI C quoting:
nl=$'\n'
And then use sed
with appropriate quoting and escapes to insert the literal \n
:
echo "123." | sed 's/\./'"\\${nl}"'next line/'
123
next line
Or use awk
:
$ echo "123." | awk '/^[[:digit:]]+\./{sub(/\./,"\nnext line")} 1'
123
next line
Or use GNU sed which supports \n
how to insert newline in a already existing BASH string on the command line
You can use two shortcuts to do that ctrl + k
and ctrl + y
:
echo "some command" && echo "some other long command"
Now move cursor somewhere (in my example, cursor is marked by >
):
echo "some command" && > echo "some other command"
Now press ctrl + k
- this will cut everything after a cursor:
echo "some command" && >
Now put \
(backslash) and press enter
:
echo "some command" && \
>
And now paste the part you've previously cut by ctrl + y
:
echo "some command" && \
echo "some other long command"
Edit: to move more easily around in a long command, you can use shortcuts:
alt + b
- move one word backwards (on Mac OS X:ESC + b
)alt + f
- move one word forwards (on Mac OS X:ESC + f
)
Ultra-solution
You can also open current line in a editor using Ctrl-x + Ctrl-e
(two shortcuts, one after another). Then edit it just as a regular text file, save & quit and voila, edited command will execute.
If you want to choose which editor to use, just set EDITOR
environment variable.
How can I insert a new line after each character in shell script?
There is a tool called fold
which inserts linebreaks, and you can tell it do add one after every character:
$ fold -w 1 <<< 'abcdefghi'
a
b
c
d
e
f
g
h
i
<<<
is used to indicate a here string. If your shell doesn't support that, you can pipe to fold
instead:
echo 'abcdefghi' | fold -w 1
Echo newline in Bash prints literal \n
Use printf
instead:
printf "hello\nworld\n"
printf
behaves more consistently across different environments than echo
.
How do I add a line break for read command?
I like Huang F. Lei's answer, but if you don't like the literal line break, this works:
read -p "Please Enter a Message: `echo $'\n> '`" message
Shows:
Please Enter a Message:
> _
...where _
is where the cursor ends up. Note that since trailing newlines are usually dropped during command substitution, I've included the >
afterward. But actually, your original question doesn't seem to want that prompt bit, so:
# Get a carriage return into `cr` -- there *has* to be a better way to do this
cr=`echo $'\n.'`
cr=${cr%.}
# Use it
read -p "Please Enter a Message: $cr" message
Shows
Please Enter a Message:
_
There has to be a better way, though.
Linux script to insert in a file a new line
try this;
sed -i '/# Load Java configuration/i JBOSS_User=root' ITg.conf
before this command, make backup as below;
cp ITg.conf ITg.conf_backup
man sed :
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
Related Topics
How to Check All Columns Data Types of Table Using Awk Script
How to Move All Files Including Hidden Files into Parent Directory via *
List Files Over a Specific Size in Current Directory and All Subdirectories
Managing User Configuration Files Across Multiple Computers
How to Diff a Directory for Only Files of a Specific Type
How to Open a "-" Dashed Filename Using Terminal
View Multi-Core or Mlti-Cpu Utlization on Linux
How to Add a Custom Footer to Sphinx Documentation? (Restructuredtext)
How to Do the Opposite of Diff
Docker Ignores Limits.Conf (Trying to Solve "Too Many Open Files" Error)
Ansible Inside Script Command Not Found
Any Way to Exit Bash Script, But Not Quitting the Terminal
I Want to Contribute to the Linux Kernel
Linux: Set Permission Only to Directories
Difference Between Checkout and Export in Svn
How to Fetch Java Version Using Single Line Command in Linux
How to Fix Symbol Lookup Error: Undefined Symbol Errors in a Cluster Environment