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

FAQ
It is currently Fri Dec 14, 2018 12:12 am


Username:
Subject:
Message body:
Enter your message here, it may contain no more than 60000 characters. 

Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
Font size:
 
Font colour
Options:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation code
Confirmation code:
In an effort to prevent automatic submissions, we require that you enter both of the words displayed into the text field underneath.
     

Topic review - How to echo newline in Linux/Unix shell
Author Message
Post subject: How to echo newline in Linux/Unix shell  |  Post Posted: Tue Jun 25, 2013 5:58 am
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.
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