last argument expansion has different output using the sh interpreter
I tried to send this using bash-bug, however I didn't realize my sendmail was not setup, so here is the description in the format of bash-bug. Bash Version: 4.2 Patch Level: 10 Release Status: release Description: When executing a test script using the #!/bin/sh interpreter line the shell expansion for the last argument ${!#} is blank, however when using #!/bin/bash the behavior is as expected. In this setup /bin/sh is a link to /bin/bash. Repeat-By: Using the following test script: #!/bin/sh FIRSTARG=$1 FINALARG=${!#} echo $FIRSTARG echo $FINALARG Output using #!/bin/sh [hickersonj@fedora15 ~]$ ./test.sh first last first Output using #!/bin/bash [hickersonj@fedora15 ~]$ ./test.sh first last first last Machine: i386 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i386' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i386-redhat-linux-gnu' -DCONF_VENDOR='redhat' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -D_GNU_SOURCE -DRECYCLES_PIDS -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables uname output: Linux fedora15.localdomain 2.6.38.6-27.fc15.i686 #1 SMP Sun May 15 17:57:13 UTC 2011 i686 i686 i386 GNU/Linux Machine Type: i386-redhat-linux-gnu Thanks, Jacoby
Re: last argument expansion has different output using the sh interpreter
On Fri, May 27, 2011 at 1:20 PM, DJ Mills wrote: > > > On Fri, May 27, 2011 at 3:45 PM, Greg Wooledge wrote: > >> On Fri, May 27, 2011 at 12:35:12PM -0700, Jacoby Hickerson wrote: >> > When executing a test script using the #!/bin/sh interpreter >> line >> > the shell expansion for the last argument ${!#} is blank, >> >> Good. And now you know why you use a proper #!/bin/bash shebang when >> you use bash extensions in your script. >> >> If you are using #!/bin/sh then you should use only POSIX sh features. >> >> > Indeed, indirection is a bash feature. I wouldn't expect it to work in > POSIX sh. > > The only way I can think of to get the last argument in sh is to loop > through them, > something like: > for arg; do last="$arg"; done; echo "$last" > Ah I see that inderection is a bash specific feature, in general I should update all my scripts. Although, I am curious, is this is a matter of sh being continually updated to exclude all bash extensions or perhaps previously bash didn't interpret #!/bin/sh to be the POSIX compliant interpreter?
Re: last argument expansion has different output using the sh interpreter
On Fri, 2011-05-27 at 16:53 -0400, Greg Wooledge wrote: > On Fri, May 27, 2011 at 01:35:50PM -0700, Jacoby Hickerson wrote: > > Although, I am curious, is this is a matter of sh > > being continually updated to exclude all bash extensions > > Eh? POSIX is defined by a group of people and published. Here's the > current edition: > > http://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html > > "... continually updated to exclude bash extensions..." doesn't even make > sense to me. It doesn't generally get *smaller* with each edition; > rather, it gets larger. A simpler question that I could have asked would be "Why did it work before?" The now obvious answer is that it shouldn't have worked before. I just didn't know how sh evolves, now it is much more clear thanks. > > or perhaps > > previously bash didn't interpret #!/bin/sh to be the POSIX compliant > > interpreter? > > Are you talking specifically about the behavior of bash when it is invoked > as "sh"? If I were you, I wouldn't even waste brain cycles on that. > When you write a script, decide whether you are writing a bash script, > or an sh script. If you are writing a bash script, pick which version of > bash you are writing it for, and then use the features available in that > version and older, and use the #!/usr/bin/env bash or #!/bin/bash shebang. > If you are writing for sh, then use only POSIX features, and use the > #!/bin/sh shebang. It was being executed from within a bash shell but using #!/bin/sh shebang inside the script. I understand that the shebang will call out the desired interpreter i.e. #!/usr/bin/perl, I was simply mistaken in thinking that sh was equivalent to bash.
Re: last argument expansion has different output using the sh interpreter
On Fri, 2011-05-27 at 14:57 -0600, Bob Proulx wrote: > Jacoby Hickerson wrote: > > Although, I am curious, is this is a matter of sh being continually > > updated to exclude all bash extensions or perhaps previously bash > > didn't interpret #!/bin/sh to be the POSIX compliant interpreter? > > When bash is invoked as sh then bash complies with the POSIX sh. That > is both required and desired. > > On some GNU/Linux systems /bin/sh is a symlink to /bin/bash and bash > will support running as a POSIX sh. But this isn't universal. On > other systems /bin/sh is an actual native POSIX sh, on others it is a > symlink to /bin/ksh or /bin/zsh or /bin/dash or others. But in all > cases /bin/sh is supposed to be a POSIX sh and conform to the > standard. Bash tries to be compliant but some differences will creep > in regardless. Doing so may cause problems for people who unknowingly > use features on one system that are not available on other systems. > Generally the wisdom of years is not to be generous in what you accept > but to be strict in what you accept. It makes portability easier. > > Having found that the bash specific feature isn't available in /bin/sh > this is a good thing for you since now your script will either need to > specify that it needs bash explicitly or you could use portable only > constructs and continue to use /bin/sh. Personally I use bash for my > command line but /bin/sh and portable constructs for shell scripts. > > Bob > Yes in this environment it is a link. It certainly makes sense to write it in a POSIX compliant way in order to be portable, but some extended features of bash make life easier. Since this was an upgrade from a previous distro Fedora 14 -> Fedora 15, I didn't think portability with a shell script would be an issue, but now it is much more clear after I have been bitten :). Your response is very excellent information and thanks to you all for the wealth of data. Thanks! Jacoby