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

  • Follow


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.
0
Reply pengyu.ut (734) 11/19/2009 8:33:29 PM

In article 
<bcd68928-cdca-4849-a626-20709ee196b9@l2g2000yqd.googlegroups.com>,
 "PengYu.UT@gmail.com" <pengyu.ut@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.

printf "\n" >/tmp/tmp

-- 
Barry Margolin, barmar@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 ***
0
Reply Barry 11/19/2009 8:46:51 PM


On November 19, 2009 15:33, in comp.unix.shell, PengYu.UT@gmail.com
(pengyu.ut@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.



-- 
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.         ------


0
Reply Lew 11/19/2009 8:54:03 PM

On 2009-11-19, PengYu.UT@gmail.com <pengyu.ut@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-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
0
Reply Seebs 11/19/2009 9:27:36 PM

On Nov 19, 2:54=A0pm, 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
> =A0 echo >/tmp/tmp
> or
> =A0 echo -e '\n' >/tmp/tmp
>
> Note that echo is not only a binary, but it is also a shell builtin comma=
nd.
> Also note that the echo binary has several system-dependant variations, a=
nd
> 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
0
Reply PengYu 11/19/2009 9:49:13 PM

On November 19, 2009 16:49, in comp.unix.shell, PengYu.UT@gmail.com
(pengyu.ut@gmail.com) wrote:

> On Nov 19, 2:54=C2=A0pm, 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?
>=20
> $ ./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 t=
hat
the shell interpreter knows that it is part of the commandline, rather =
than
the end-of-command marker)

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


0
Reply Lew 11/19/2009 10:01:53 PM

On 2009-11-19, PengYu.UT@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.

   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    =====
0
Reply Chris 11/19/2009 10:08:44 PM

6 Replies
1570 Views

(page loaded in 0.077 seconds)

Similiar Articles:













7/20/2012 4:02:48 PM


Reply: