Skip to content

Instantly share code, notes, and snippets.

@sec-fortress
Created July 5, 2024 12:51
Show Gist options
  • Save sec-fortress/22a18ecba4fe03028892c1120bdbb82d to your computer and use it in GitHub Desktop.
Save sec-fortress/22a18ecba4fe03028892c1120bdbb82d to your computer and use it in GitHub Desktop.
Difference between running a particular script with ./ or bash, perl, python (Whatever executable language)

Running a script with ./ or bash involves different ways of executing the script, which can have implications for the environment and the way the script is processed. Here’s a detailed comparison:

Running a Script with ./

When you run a script using ./script.sh, you are telling the shell to execute the script file directly. This requires the script to:

  1. Have Execute Permissions: The script file must be executable. You can set this with chmod +x script.sh.
  2. Specify the Interpreter: The first line of the script should specify the interpreter to be used (e.g., #!/bin/bash for a bash script, #!/usr/bin/env python3 for a Python script). This is known as the shebang line.

Example:

#!/bin/bash

echo "Running with ./"
chmod +x script.sh 
./script.sh

Running a Script with bash

When you run a script using bash script.sh, you are explicitly invoking the Bash shell to execute the script. In this case, the execute permission is not required, and the shebang line is ignored (though it's good practice to include it for portability).

Example:

#!/bin/bash

echo "Running with bash"
bash script.sh

Key Differences

  1. Permissions:

    • ./script.sh: Requires execute permissions (chmod +x).
    • bash script.sh: Does not require execute permissions.
  2. Interpreter:

    • ./script.sh: Uses the interpreter specified in the shebang line (e.g., #!/bin/bash).
    • bash script.sh: Explicitly uses the bash interpreter, ignoring the shebang line.
  3. Environment:

    • ./script.sh: Runs in a new shell instance specified by the shebang.
    • bash script.sh: Runs in a new Bash shell instance.
  4. Portability:

    • ./script.sh: The shebang line makes it clear which interpreter should be used, which can be important for scripts that need to run on different systems.
    • bash script.sh: Assumes the Bash shell is available and used to interpret the script.

Process Differences

When you run a script, it generally starts a new process. The main difference is how this process is started and which interpreter is used:

  • ./script.sh: The operating system creates a new process and uses the interpreter specified in the shebang line to execute the script.
  • bash script.sh: The bash command starts a new process and interprets the script using Bash, regardless of the shebang line.

In summary, ./script.sh relies on the script having execute permissions and uses the shebang to determine the interpreter, while bash script.sh explicitly uses Bash to run the script without needing execute permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment