Results 1 to 7 of 7
A stripped-down example script that demonstrates the behavior that's befuddling me:
Code:
#!/bin/sh
alias foo=bar
foo
returns "bar: command not found", which is what I expect that script to return. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 06-09-2006 #1Just Joined!
- Join Date
- Jun 2006
- Location
- Portland, Oregon, USA
- Posts
- 1
Why does a #!/bin/bash script handle aliasing worse than a #!/bin/sh script?
A stripped-down example script that demonstrates the behavior that's befuddling me:
returns "bar: command not found", which is what I expect that script to return. On the other hand,Code:#!/bin/sh alias foo=bar foo
returns "foo: command not found", as if the alias command was ignored.Code:#!/bin/bash alias foo=bar foo
If anything, I'd expect the /bin/sh version of the script to ignore the alias, for possible compatibility with old scripts written for a version of /bin/sh that wasn't just a link to bash.
Could someone explain this behavior to me, please? It happens on more than one Linux distro, and even on Cygwin's bash under Windows (where /bin/sh is a copy of /bin/bash, not a symbolic link to it).
UmberGryphon
(who will just start his script with #!/bin/sh in the meantime)
- 06-10-2006 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi.
So forAliases are not expanded when the shell is not interactive, unless the
expand_aliases shell option is set using shopt (see the description of
shopt under SHELL BUILTIN COMMANDS below ...
-- man bash
We would see:Code:#!/bin/bash # @(#) s2 Demonstrate alias. shopt -s expand_aliases alias foo=bar foo
cheers, drlCode:$ ./s2 ./s2: line 7: bar: command not found
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 06-10-2006 #3Just Joined!
- Join Date
- May 2006
- Posts
- 31
You really shouldn't use aliases in scripts anyway. Write functions instead.
Just my opinion though.
- 06-10-2006 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi, vlsimpson.
Always glad to see another viewpoint -- is there any specific reason behind your opinion -- performance, maintenance, etc? ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 06-10-2006 #5Just Joined!
- Join Date
- May 2006
- Posts
- 31
Just more versatile mainly.
Originally Posted by drl
- 07-12-2006 #6Just Joined!
- Join Date
- Jul 2006
- Posts
- 1
Is there any bash Version Compatibility
I do found the same problem.
Is there any Version Compatibility
Either upgrade the version of bash or run configure under another shell
(such as /bin/sh).
Other suggestion of using function is good, as maual says
"For almost every purpose, shell functions are preferred over aliases. "
- 07-12-2006 #7Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi, ajayh.
What specific advantages have you found to favor the use functions over aliases? ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
