Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 12 Dec 2002 09:09:04 +0000
From:      Matthew Seaman <m.seaman@infracaninophile.co.uk>
To:        FreeBSD Questions <freebsd-questions@FreeBSD.ORG>
Subject:   Re: Newline character doesn't work
Message-ID:  <20021212090904.GA46326@happy-idiot-talk.infracaninophi>
In-Reply-To: <c42a6dc3ea00.c3ea00c42a6d@mbox.com.au>
References:  <c42a6dc3ea00.c3ea00c42a6d@mbox.com.au>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Dec 12, 2002 at 07:14:28PM +1100, BSD Freak wrote:

> Just tried the following and it doesn't seem to work.. any explanations?
> 
> %echo first line \\n second line
> 
> Output is: 
> first line \n second line

echo is both a shell built-in and a standard command.  The standard
command doesn't support expanding character escapes like '\n':

    % /bin/echo "line one\nline two"
    line one\nline two

This is a difference between the BSD echo(1) command and the SYSV
equivalent.

Of the shells bundled with FreeBSD, /bin/sh uses the standard command
version and /bin/tcsh uses a shell built-in that by default has the
same behaviour as /bin/echo:

    % echo $SHELL
    /bin/tcsh
    % echo "line one\nline two"
    line one\nline two

However you can set the 'echo_style' shell variable to make the tcsh(1)
built-in behave in the SysV style:

    % set echo_style=sysv
    % echo "line one\nline two"
    line one
    line two

Other shells have alternate mechanisms for achieving the same thing,
eg:

bash(1) uses a '-e' flag to the echo command to enable SysV style

    bash-2.05b$ echo "line one\nline two"
    line one\nline two
    bash-2.05b$ echo -e "line one\nline two"
    line one
    line two

whereas zsh(1) expands character escapes by default:

    happy-idiot-talk% echo $ZSH_VERSION
    4.0.6
    happy-idiot-talk% echo "line one\nline two" 
    line one
    line two

In this case you can use the '-E' flag to make echo behave in the BSD
way:

    % echo -E "line one\nline two"
    line one\nline two

If you want something that will behave consistently independent of
whatever shell is being used, try the printf(1) command:

    % printf "line one\nline two\n"
    line one
    line two

(Note: no automatic addition of newline after the last character)

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                       26 The Paddocks
                                                      Savill Way
                                                      Marlow
Tel: +44 1628 476614                                  Bucks., SL7 1TH UK

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021212090904.GA46326>