Open a terminal, move to the directory containing your script, and type bash script.sh. This starts a new Bash process and reads the file. If you are learning how Bash scripts run, this is the simplest way to test one without changing its execute permissions.
Run Bash Script with an Explicit Interpreter
Suppose a file named deploy.sh is in the current directory. Use:
bash deploy.sh
Bash needs read access to the file. The .sh extension is a convention; the command works with another filename too. To execute a script elsewhere, supply its path, such as bash /home/sam/tools/deploy.sh. Quote paths containing spaces: bash "my scripts/deploy.sh".
If you need to know how to run a bash script in terminal sessions with arguments, put them after the filename:
bash deploy.sh staging "release candidate"
Inside the script, $1 is staging and $2 is release candidate. Quote "$1" when expanding it to preserve spaces; "$@" passes all arguments onward as separate values. Arguments have meaning only if the script handles them.
Make the File Executable
To choose how to execute a bash script directly, add a shebang as the first line, grant execute permission, and invoke the path:
#!/usr/bin/env bash
chmod u+x deploy.sh
./deploy.sh staging
The shebang selects the interpreter for direct execution. Here, /usr/bin/env finds Bash through PATH; #!/bin/bash instead specifies a fixed location. chmod u+x adds execute permission for the file owner. The ./ prefix identifies a file in the current directory, which may not be on your command search path.
How to run Bash script files repeatedly depends on the environment: direct execution is convenient once the interpreter and permissions are correct. Executing a file found through PATH also requires that its directory be listed there. Avoid adding a writable or untrusted directory to PATH merely to save typing.
Check the Working Directory and Interpreter
The working directory is where the terminal is currently positioned, not automatically the script’s directory. Check it with pwd and inspect files with ls. A command inside the script such as cat config.txt resolves that relative path against the working directory. Either change into the intended directory before starting or make the script construct paths from a known location.
bash deploy.sh explicitly selects Bash, regardless of the shebang. Direct execution uses the interpreter named in the shebang. Running sh deploy.sh selects sh instead; on many systems it is a different shell and may reject Bash-specific syntax such as arrays. Use the interpreter the script was written for. The Bash manual explains these invocation and shebang rules.
Diagnose a Failed Start
If the terminal reports command not found, check the spelling and path; use ./deploy.sh for a file in the current directory. A No such file or directory message can mean the script path is wrong, but it can also mean the interpreter in its shebang is missing. Inspect the first line and verify that the named interpreter exists. Windows-style CRLF line endings can also leave an extra carriage return in that line; save the file with Unix LF endings.
The most likely direct-execution failure is Permission denied. Check ls -l deploy.sh for the owner’s x bit, then run chmod u+x deploy.sh if you own the file and intend to execute it. If execute permission is already present, check the directory permissions and whether the filesystem is mounted with noexec; move the trusted script to a suitable location or use bash deploy.sh when direct execution is blocked by that mount.
