Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How pass a newline character as an argument to a command? (in bash)

21 views
Skip to first unread message

peng...@gmail.com

unread,
Nov 19, 2009, 3:33:29 PM11/19/09
to
I'm wondering how to pass a newline character as an argument to a
command in bash.

echo "\n" > /tmp/tmp

I tried the above example. But I get the string '\n' (literally) in
the file.

Barry Margolin

unread,
Nov 19, 2009, 3:46:51 PM11/19/09
to
In article
<bcd68928-cdca-4849...@l2g2000yqd.googlegroups.com>,
"Peng...@gmail.com" <peng...@gmail.com> wrote:

printf "\n" >/tmp/tmp

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Lew Pitcher

unread,
Nov 19, 2009, 3:54:03 PM11/19/09
to
On November 19, 2009 15:33, in comp.unix.shell, Peng...@gmail.com
(peng...@gmail.com) wrote:

Barry gave you one solution.

You can also try
echo >/tmp/tmp
or
echo -e '\n' >/tmp/tmp

Note that echo is not only a binary, but it is also a shell builtin command.
Also note that the echo binary has several system-dependant variations, and
not all handle the (implicit) newline in the same way.

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


Seebs

unread,
Nov 19, 2009, 4:27:36 PM11/19/09
to
On 2009-11-19, Peng...@gmail.com <peng...@gmail.com> wrote:
> I'm wondering how to pass a newline character as an argument to a
> command in bash.

command "
"

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

peng...@gmail.com

unread,
Nov 19, 2009, 4:49:13 PM11/19/09
to
On Nov 19, 2:54 pm, Lew Pitcher <lpitc...@teksavvy.com> wrote:
> On November 19, 2009 15:33, in comp.unix.shell, PengYu...@gmail.com

>
> (pengyu...@gmail.com) wrote:
> > I'm wondering how to pass a newline character as an argument to a
> > command in bash.
>
> > echo "\n" > /tmp/tmp
>
> > I tried the above example. But I get the string '\n' (literally) in
> > the file.
>
> Barry gave you one solution.
>
> You can also try
>   echo >/tmp/tmp
> or
>   echo -e '\n' >/tmp/tmp
>
> Note that echo is not only a binary, but it is also a shell builtin command.
> Also note that the echo binary has several system-dependant variations, and
> not all handle the (implicit) newline in the same way.

What I want is to pass an newline character as an argument to any
program.

I made the following C++ program.
$ cat main.cc
#include <iostream>

int main(int argc, char *argv[]) {
std::cout << argv[1];
}

When I run the executable, it gives me the following. So the newline
character is not passed to the program. Is it possible to pass the
newline character to a program?

$ ./main.exe "\n"
\n

Lew Pitcher

unread,
Nov 19, 2009, 5:01:53 PM11/19/09
to
On November 19, 2009 16:49, in comp.unix.shell, Peng...@gmail.com
(peng...@gmail.com) wrote:

> On Nov 19, 2:54 pm, Lew Pitcher <lpitc...@teksavvy.com> wrote:
>> On November 19, 2009 15:33, in comp.unix.shell, PengYu...@gmail.com
>>
>> (pengyu...@gmail.com) wrote:
>> > I'm wondering how to pass a newline character as an argument to a
>> > command in bash.

[snip]


> When I run the executable, it gives me the following. So the newline
> character is not passed to the program. Is it possible to pass the
> newline character to a program?
>
> $ ./main.exe "\n"
> \n

Seebs gave you the answer to that one...
./main.exe "
"

embed a newline directly into your command (placed between quotes, so that
the shell interpreter knows that it is part of the commandline, rather than
the end-of-command marker)

Chris F.A. Johnson

unread,
Nov 19, 2009, 5:08:44 PM11/19/09
to

Take your pick:

NL='
'

NL=$'\n'

printf -v NL "\n"

etc....

--
Chris F.A. Johnson, author <http://shell.cfajohnson,com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====

SHAHIDNX

unread,
Feb 1, 2014, 6:03:49 AM2/1/14
to
I know the ansmwer.. but is this thread is active?


Bill Marcum

unread,
Feb 2, 2014, 8:24:04 PM2/2/14
to
On 02/01/2014 06:03 AM, SHAHIDNX wrote:
> I know the ansmwer.. but is this thread is active?
>
>
A Google Groups search on the message ID says it's from 2009, so that
would be a no. To answer the original question, you could use $'\n'.
0 new messages