If you’re new to Bash scripting, you might have noticed two common ways to execute a script: bash script.sh and ./script.sh. While both methods run your script, there are some key differences between them. In this tutorial, we’ll explore these differences and help you understand when to use each method.

Basic Execution Methods

a) bash script.sh
This method explicitly calls the Bash interpreter to execute your script.
b) ./script.sh
This method executes the script directly, using the shebang (#!) line in the script to determine which interpreter to use.

Interpreter Selection

a) bash script.sh

  • Always uses Bash to run the script, regardless of the shebang line.
  • Useful when you want to ensure Bash is used, even if the script specifies a different interpreter.

b) ./script.sh

  • Uses the interpreter specified in the script’s shebang line.
  • If no shebang is present, it uses the current shell.

Example:

1
2
3
#!/bin/sh
echo "This script is running in $$"
readlink /proc/$$/exe;

Running this script with bash script.sh will always use Bash, while ./script.sh will use /bin/sh as specified in the shebang.
Now, let’s see both methods to invoke this simple script:

1
2
3
$ bash script.sh 
This script is running in 31310
/usr/bin/bash
1
2
3
$ ./script.sh 
This script is running in 31312
/usr/bin/dash

Execution Permissions

a) bash script.sh

  • Does not require the script to have execute permissions.
  • Bash reads the file and executes its contents.

b) ./script.sh

  • Requires the script to have execute permissions (chmod +x script.sh).

  • You’ll get a “Permission denied” error if the script isn’t executable.

    Path Considerations

    a) bash script.sh

  • Works as long as ‘bash’ is in your PATH.

  • Can execute scripts from any directory without ‘./‘ if the script’s directory is in your PATH.

b) ./script.sh

  • The ‘./‘ specifies the current directory.

  • Necessary for security reasons, as the current directory is not typically in the PATH.

    Subshell Behavior

    a) bash script.sh

  • Always runs the script in a new subshell.

  • Changes to environment variables in the script don’t affect the parent shell.

b) ./script.sh

  • Typically runs in a subshell, but can be sourced to run in the current shell using ‘. ./script.sh’.

    Debugging

    a) bash -x script.sh

  • Enables debug mode, printing each command before execution.

  • Useful for troubleshooting scripts.

b) ./script.sh

  • To debug, you need to modify the shebang line or the script itself.

    Portability

    a) bash script.sh

  • More portable across different Unix-like systems.

  • Ensures Bash is used, which may be important for Bash-specific features.

b) ./script.sh

  • Relies on the correct interpreter being available as specified in the shebang.

  • More flexible if you want to use different interpreters for different scripts.

    Conclusion

    Both bash script.sh and ./script.sh have their places in Bash scripting. Use bash script.sh when you want to explicitly use Bash or when executing scripts without making them executable. Use ./script.sh for scripts with proper shebangs and execute permissions, especially in production environments.
    Remember:

  • Always use shebangs in your scripts for clarity.

  • Set appropriate permissions for your scripts.

  • Consider portability and environment when choosing an execution method.

By understanding these differences, you can choose the most appropriate method for executing your Bash scripts in various scenarios.