Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Sun Aug 20, 2017 4:50 am


Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

Author Message
mandrei99
Post  Post subject: How to echo newline in Linux/Unix shell  |  Posted: Tue Jun 25, 2013 5:58 am

Joined: Tue Aug 04, 2009 9:16 am
Posts: 245

Offline
 

How to echo newline in Linux/Unix shell

Many programming or scripting languages interpret characters preceded by backslash as special characters. Example "\n" as echo new line.

By default, "echo" Linux shell command does not interpret "\n". To do so, enable the special switch. Quote from "man echo":
Quote:
DESCRIPTION
Echo the STRING(s) to standard output.

-n do not output the trailing newline

-e enable interpretation of backslash escapes

-E disable interpretation of backslash escapes (default)

and look at the difference:
Code:
user@host:~> echo "New\nLine"
New\nLine
user@host:~> echo -e "New\nLine"
New
Line

Echo newline one or multiple times - ECHO


To echo newline, make use of the -e echo argument, the "echo" command will start to interpret escaped characters: \n as LineFeed (newline in Unix) or \r\n (newline in Windows systems). Further info at http://en.wikipedia.org/wiki/Newline.
Code:
user@linux:~# echo -e "Two newlines following this\n\nAnother sentence"
Two new lines following this

Another sentence

Note: to echo new line with the '\n' character, you must be in bash shell.

Echo newline with PRINTF


Code:
$ printf "line1\nline2\n"
line1
line2

Quote:
PRINTF(1) BSD General Commands Manual PRINTF(1)

NAME
printf -- formatted output

SYNOPSIS
printf format [arguments ...]

DESCRIPTION
The printf utility formats and prints its arguments, after the first, under control of the format. The format is a character string
which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences which
are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive
argument.

The arguments after the first are treated as strings if the corresponding format is either c, b or s; otherwise it is evaluated as a C
constant, with the following extensions:

o A leading plus or minus sign is allowed.
o If the leading character is a single or double quote, the value is the ASCII code of the next character.

The format string is reused as often as necessary to satisfy the arguments. Any extra format specifications are evaluated with zero
or the null string.

Character escape sequences are in backslash notation as defined in the ANSI X3.159-1989 (``ANSI C89''), with extensions. The charac-
ters and their meanings are as follows:

\a Write a <bell> character.
\b Write a <backspace> character.
\c Ignore remaining characters in this string.
\f Write a <form-feed> character.
\n Write a <new-line> character.
\r Write a <carriage return> character.
\t Write a <tab> character.
\v Write a <vertical tab> character.
\' Write a <single quote> character.
\\ Write a backslash character.
\num
\0num Write an 8-bit character whose ASCII value is the 1-, 2-, or 3-digit octal number num.

Printf (print formatted) is another method to echo new line in Linux shell or scripts. Printf is an utility similar to C/Perl and it accepts formatted text like \n for new line and also can print color codes.

Supress newline in echo


In the default form, the command will append a new line at the end of every echoed text. This can be suppressed using the -n option:
Code:
$ echo -n oneword
oneword$

P.S.: Supressing new line in echo is used by startup scripts in Unix to display the progress in dots.





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "How to echo newline in Linux/Unix shell"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. Unix shell - using TR to replace new lines with spaces

debuser

0

4856

Thu Feb 25, 2010 8:08 am

debuser View the latest post

There are no new unread posts for this topic. How to add line numbers in unix shell output

mandrei99

0

841

Fri Oct 31, 2014 5:31 am

mandrei99 View the latest post

There are no new unread posts for this topic. Echo new line in linux - supress new line or echo multiple new lines

mandrei99

0

2788

Wed Jan 14, 2015 5:26 pm

mandrei99 View the latest post

There are no new unread posts for this topic. AWK: How to replace a newline with actual \n

admin

0

54

Thu Jul 20, 2017 11:02 am

admin View the latest post

There are no new unread posts for this topic. Remove the character before the last character with SED - shell scripting

mandrei99

0

1476

Wed Nov 04, 2009 10:49 am

mandrei99 View the latest post

There are no new unread posts for this topic. How to increase bash history size in Linux

mandrei99

0

8386

Mon Jan 12, 2015 11:45 am

mandrei99 View the latest post

There are no new unread posts for this topic. Linux / FreeBSD: Avoid bash history duplicate lines with HISTCONTROL

mandrei99

0

546

Mon Jan 12, 2015 12:01 pm

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO