|
Home > Archive > Unix Shell > August 2007 > -n option for bash script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
-n option for bash script
|
|
|
| Hi all,
I'm encountering something which I don't quite understand. On
version 3.1.17(1) of GNU bash (for Debian stable), I made this very
simple script:
-----
#!/bin/bash
echo $#
echo $1
echo $*
echo $@
-----
And then I ran it as follows:
>testscript
0
>testscript -n -x
2
-x-x>testscript -x -n
2
-x
-x -n
-x -n
>testscript foo -n -x
3
foo
foo -n -x
foo -n -x
Everything is expected except when "-n" is the first option. It says
(correctly) that there are two options (-n and -x), but "-n" is
dropped and disappears. Is there something special about "-n"? I
tried googling for this but funny enough "-n" is dropped in Google
searches and I'm not sure what this problem is called so that I can
enter it in Google...
Any help would be appreciated; thanks! (And yes, I realize that one
solution is to just not use "-n"...but I'd like to know why or what
I'm doing wrong. Perhaps my installation of bash is too old?) I was
thinking that perhaps "-n" is some kind of control character that is
being translated to something that is unprintable...but that's
something I've never heard of...
Ray
| |
| John L 2007-08-30, 7:20 am |
|
"Ray" <raykyoto@gmail.com> wrote in message news:1188461218.861452.62820@x40g2000prg.googlegroups.com...
> Hi all,
>
> I'm encountering something which I don't quite understand. On
> version 3.1.17(1) of GNU bash (for Debian stable), I made this very
> simple script:
>
> -----
> #!/bin/bash
>
> echo $#
> echo $1
>
> echo $*
>
> echo $@
> -----
>
> And then I ran it as follows:
>
> 0
>
>
>
> 2
> -x-x>testscript -x -n
> 2
> -x
> -x -n
> -x -n
> 3
> foo
> foo -n -x
> foo -n -x
>
> Everything is expected except when "-n" is the first option. It says
> (correctly) that there are two options (-n and -x), but "-n" is
> dropped and disappears. Is there something special about "-n"?
The question you need to ask is what "echo -n" does: man echo
for details.
--
John.
| |
|
| On Aug 30, 11:06 am, Ray <rayky...@gmail.com> wrote:
> Hi all,
>
> I'm encountering something which I don't quite understand. On
> version 3.1.17(1) of GNU bash (for Debian stable), I made this very
> simple script:
>
> -----
> #!/bin/bash
>
> echo $#
> echo $1
>
> echo $*
>
> echo $@
> -----
>
> And then I ran it as follows:
>
>
> 0
>
>
> 2
> -x-x>testscript -x -n
help echo
If -n is specified, the trailing newline is suppressed.
the -n is interpreted by echo
| |
|
|
Hi John and pgas,
On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote:
> help echo
> If -n is specified, the trailing newline is suppressed.
>
> the -n is interpreted by echo
I see...thank you both for the explanation! So, this is just a
special case for bash scripts? That if -n is the first parameter, it
gets picked up by echo? Looking at "help echo", it seems that echo
takes 3 arguments (-n, -e, and -E), so these three are special cases
that one has to remember?
Is there anything else to be aware of? Where would one find this
information in the man pages? I looked at "man bash" and nothing
seems to indicate the link between bash scripts and echo.
Thanks a lot!
Ray
| |
| Joachim Schmitz 2007-08-30, 1:20 pm |
| "Ray" <raykyoto@gmail.com> schrieb im Newsbeitrag
news:1188488030.994888.253240@x35g2000prf.googlegroups.com...
>
> Hi John and pgas,
>
> On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote:
>
> I see...thank you both for the explanation! So, this is just a
> special case for bash scripts? That if -n is the first parameter, it
> gets picked up by echo? Looking at "help echo", it seems that echo
> takes 3 arguments (-n, -e, and -E), so these three are special cases
> that one has to remember?
Yes
> Is there anything else to be aware of? Where would one find this
> information in the man pages? I looked at "man bash" and nothing
> seems to indicate the link between bash scripts and echo.
because ther is none, it's not a bash thing, it echo 'eating' the -n, not
bash.
use
echo -- $1
echo -- $*
echo -- $@
the -- should tell echo (and any other command) that no options are to
follow.
Bye, Jojo
| |
| Chris F.A. Johnson 2007-08-30, 7:21 pm |
| On 2007-08-30, Ray wrote:
>
> Hi John and pgas,
>
> On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote:
>
> I see...thank you both for the explanation! So, this is just a
> special case for bash scripts? That if -n is the first parameter, it
> gets picked up by echo?
It doesn't "get picked up by echo"; it is an argument to echo that
may be (and is, by bash) interpreted as an option. Not all
versions of echo regard -n as an option.
> Looking at "help echo", it seems that echo
> takes 3 arguments (-n, -e, and -E), so these three are special cases
> that one has to remember?
The command can take as many arguments as you give it, subject to
system limitations. Those arguments that begin with a hyphen may
or may not be interpreted as options.
The POSIX standard says that echo does not support any options;
some shells do accept options to echo.
> Is there anything else to be aware of? Where would one find this
> information in the man pages? I looked at "man bash" and nothing
> seems to indicate the link between bash scripts and echo.
Read the entry for echo in the 'SHELL BUILTIN COMMANDS' section
of the man page.
If you do not have complete control over the text you want to
print, do not use echo; use printf instead.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Chris F.A. Johnson 2007-08-30, 7:21 pm |
| On 2007-08-30, Joachim Schmitz wrote:
....
> use
> echo -- $1
> echo -- $*
> echo -- $@
>
> the -- should tell echo (and any other command) that no options are to
> follow.
That usage is contrary to the POSIX specification, which says:
The echo utility shall not recognize the "--" argument in the
manner specified by Guideline 10 of the Base Definitions...
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Barry Margolin 2007-08-30, 7:21 pm |
| In article <1188488030.994888.253240@x35g2000prf.googlegroups.com>,
Ray <raykyoto@gmail.com> wrote:
> Hi John and pgas,
>
> On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote:
>
> I see...thank you both for the explanation! So, this is just a
> special case for bash scripts? That if -n is the first parameter, it
> gets picked up by echo? Looking at "help echo", it seems that echo
> takes 3 arguments (-n, -e, and -E), so these three are special cases
> that one has to remember?
There's nothing "special" going on. Your script contains the command:
echo $*
and $* gets replaced with the parameters you gave to the script. So if
you do:
testscript -n -x
then that command becomes:
echo -n -x
> Is there anything else to be aware of? Where would one find this
> information in the man pages? I looked at "man bash" and nothing
> seems to indicate the link between bash scripts and echo.
It's not even specific to echo. Put the following line in your script:
cat "$@"
and then run:
testscript -n -x
--
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 ***
| |
|
|
Hi all,
On Aug 31, 9:18 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <1188488030.994888.253...@x35g2000prf.googlegroups.com>,
>
> Ray <rayky...@gmail.com> wrote:
> There's nothing "special" going on. Your script contains the command:
>
> echo $*
>
> and $* gets replaced with the parameters you gave to the script. So if
> you do:
>
> testscript -n -x
>
> then that command becomes:
>
> echo -n -x
Ah, I see now! I was mistakenly thinking that arguments to scripts
are "special" and somehow, echo (and other commands in a script)
wouldn't treat them as arguments to themselves. I see where I was
wrong now...thank you! As I don't have control over what is passed to
the script, I think I will stick to cat, printf, etc.
Ray
| |
| Joachim Schmitz 2007-08-31, 1:19 pm |
| "Chris F.A. Johnson" <cfajohnson@gmail.com> schrieb im Newsbeitrag
news:pvojq4-3kl.ln1@xword.teksavvy.com...
> On 2007-08-30, Joachim Schmitz wrote:
> ...
>
> That usage is contrary to the POSIX specification, which says:
>
> The echo utility shall not recognize the "--" argument in the
> manner specified by Guideline 10 of the Base Definitions...
pitty. Sorry, I didn't check before posting.
Bye, Jojo
|
|
|
|
|