Find the answer to your Linux question:
Results 1 to 6 of 6
Hey is it common to use Code: #!/bin/bash -i And what is the correct method to run a .sh file? Code: $ bash $ . $ ./...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2013
    Posts
    2

    BASH run & -i


    Hey is it common to use

    Code:
    #!/bin/bash -i
    And what is the correct method to run a .sh file?

    Code:
     
    $ bash
    
    $ .
    
    $ ./
    Last edited by vocal300; 03-05-2013 at 12:35 AM. Reason: adjust code

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    679
    For a script to run there are three things that need to be done.

    1) In the script itself it must start with a special comment (referred to as the she-bang line or hack). This is a comment line in the script as the first line:
    Code:
    #!/bin/bash
    This tells the program that reads and runs the script (in this case bash which is found in "/bin/bash").

    2) The permissions need to be changed to allow it to be executed. Here some examples of changing the permissions of a file:
    Code:
    chmod 755 filename
    
    chmod u+x filename
    3) Place the file in a directory that you have added to your PATH environment variable or include the path when executing the script (or program binary if using C, C++, FORTRAN, ADA, etc).

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,444
    Addendum:
    1) Consider using
    Code:
    #!/usr/bin/env bash
    Not all unixes have bash in /bin/ , but all have /usr/bin/env.

    3) Should you place your script in the system $PATHs, then my advise is to do it controlled. Aka by wrapping your script in a rpm/deb package.
    You must always face the curtain with a bow.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Mar 2013
    Posts
    2
    Ok

    Back to my question.

    I have a script. It works fine with either #!/usr/bin/env bash or #!/bin/bash. But I have another script using the history command, Yes I read it's disabled. But when I run it as . scriptfile.sh or source scriptfile.sh it works 100%. Why?

    And why all the different ways to run scripts? Say I want to share with Mary, Paul & Tara. I don't want them to request changing environment variables to make this work.


  6. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    679
    This method should never be used for admin scripts that might be used during the boot process as "/usr" may not be mounted. Yes I know udev is breaking this but that does not make it correct. That is why login shells suxh as bash are in "/bin/".

  7. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    679
    Quote Originally Posted by vocal300 View Post
    Ok

    Back to my question.

    I have a script. It works fine with either #!/usr/bin/env bash or #!/bin/bash. But I have another script using the history command, Yes I read it's disabled. But when I run it as . scriptfile.sh or source scriptfile.sh it works 100%. Why?

    And why all the different ways to run scripts? Say I want to share with Mary, Paul & Tara. I don't want them to request changing environment variables to make this work.

    The "." or "source" says to read the file and execute the statements as if they were just typed in from stdin (standard in).

    When you run a script the shell (bash most likey but there are many others) loads a new shell and runs the script.

Posting Permissions

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