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:
When you run a script using ./script.sh, you are telling the shell to execute the script file directly. This requires the script to:
- Have Execute Permissions: The script file must be executable. You can set this with
chmod +x script.sh. - Specify the Interpreter: The first line of the script should specify the interpreter to be used (e.g.,
#!/bin/bashfor a bash script,#!/usr/bin/env python3for a Python script). This is known as the shebang line.