|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"Ray" <raykyoto@gmail.com> wrote in message news:1188461218.861452.62820@x40g2000prg.googlegro ups.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: > > >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"? The question you need to ask is what "echo -n" does: man echo for details. -- John. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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: > > >testscript > > 0 > > >testscript -n -x > > 2 > -x-x>testscript -x -n echo If -n is specified, the trailing newline is suppressed. the -n is interpreted by echo |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Hi John and pgas, On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote: > 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 " 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"Ray" <raykyoto@gmail.com> schrieb im Newsbeitrag
news:1188488030.994888.253240@x35g2000prf.googlegr oups.com... > > Hi John and pgas, > > On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote: >> 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 " 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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 2007-08-30, Ray wrote:
> > Hi John and pgas, > > On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote: >> 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? 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 " 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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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: > > 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 " 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 *** |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
"Chris F.A. Johnson" <cfajohnson@gmail.com> schrieb im Newsbeitrag
news:pvojq4-3kl.ln1@xword.teksavvy.com... > 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... pitty. Sorry, I didn't check before posting. Bye, Jojo |
|
![]() |
| Outils de la discussion | |
|
|