Find the answer to your Linux question:
Results 1 to 7 of 7
Greetings I have a general question. I just began my first lesson in Bash programming. If I am in the Bash shell, why would my script not run unless preceded ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie BoDiddley's Avatar
    Join Date
    Oct 2010
    Location
    Plainfield, New Jersey
    Posts
    137

    Bash


    Greetings I have a general question. I just began my first lesson in Bash programming.

    If I am in the Bash shell, why would my script not run unless preceded by /bin/bash?

    When I do "set" to list my variables it says:

    BASH=/bin/bash

    Yet if I make the script executable and do not precede it with /bin/bash - Bash tells me "command not found".

    Not real important, just curious.

    I am learning from a book, by the way.

    Bash version is 3.2.39(1)release

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,383
    Because there are a lot of script languages: php, perl, python, various shells, etc.
    The Shebang tells the program loader, which interpreter to load, which in turn is given the script to execute.

    Little trick to gain portability:
    Code:
    #!/bin/env bash
    This will work on linux (/bin/bash) as well as e.g. a BSD (/usr/local/bin/bash)
    Last edited by Irithori; 09-11-2011 at 09:10 PM.
    You must always face the curtain with a bow.

  3. #3
    Linux Newbie BoDiddley's Avatar
    Join Date
    Oct 2010
    Location
    Plainfield, New Jersey
    Posts
    137
    Got it. I did read that, but did not digest it. Thanks.

  4. #4
    Linux Newbie BoDiddley's Avatar
    Join Date
    Oct 2010
    Location
    Plainfield, New Jersey
    Posts
    137
    Ok.Nothing works but /bin/bash in front of the executable name. The cheat code in the script does not work.

    For now I will execute the script preceded by /bin/bash. I know the shells are standardized, bit confusing why I cannot put it in the script. Either the referenced material or your cheat.

    I am Debian. Script created in Vi.

  5. #5
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,383
    Can you please paste the shebang of your script in Code tags?

    The head should look similar this:
    Code:
    vi first_script.sh
    
    write this and save:
    #!/bin/env bash
    echo "first script"
    
    chmod 750 first_script.sh
    ./first_sript.sh
    You must always face the curtain with a bow.

  6. #6
    Linux Newbie BoDiddley's Avatar
    Join Date
    Oct 2010
    Location
    Plainfield, New Jersey
    Posts
    137
    Thanks. I was not using the . /

    When using . / I was able to run it with #!/bin/sh as first line. But not #!/bin/env bash "/bin/env - bad interpreter".

    I can either use /bin/bash before the executable. Or #!/bin/sh and . /

    My first lesson was success. Putting the book away for now. Thanks for your assist. Thanks for the chmod cheat, too.

  7. #7
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,383
    yw.

    Oops, should have been
    #!/usr/bin/env bash
    You must always face the curtain with a bow.

Posting Permissions

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