Find the answer to your Linux question:
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.
  1. #1
    Just 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:

    Code:
    #!/bin/sh
    alias foo=bar
    foo
    returns "bar: command not found", which is what I expect that script to return. On the other hand,

    Code:
    #!/bin/bash
    alias foo=bar
    foo
    returns "foo: command not found", as if the alias command was ignored.

    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)

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.
    Aliases 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
    So for
    Code:
    #!/bin/bash
    
    # @(#) s2       Demonstrate alias.
    
    shopt -s expand_aliases
    alias foo=bar
    foo
    We would see:
    Code:
    $ ./s2
    ./s2: line 7: bar: command not found
    cheers, drl
    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 )

  3. #3
    Just Joined!
    Join Date
    May 2006
    Posts
    31
    You really shouldn't use aliases in scripts anyway. Write functions instead.

    Just my opinion though.

  4. $spacer_open
    $spacer_close
  5. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi, vlsimpson.

    Always glad to see another viewpoint -- is there any specific reason behind your opinion -- performance, maintenance, etc? ... cheers, drl
    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 )

  6. #5
    Just Joined!
    Join Date
    May 2006
    Posts
    31
    Quote Originally Posted by drl
    Hi, vlsimpson.

    Always glad to see another viewpoint -- is there any specific reason behind your opinion -- performance, maintenance, etc? ... cheers, drl
    Just more versatile mainly.

  7. #6
    Just Joined!
    Join Date
    Jul 2006
    Posts
    1

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

  8. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi, ajayh.

    What specific advantages have you found to favor the use functions over aliases? ... cheers, drl
    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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •