How to Run and Debug Bash Scripts on Linux

Updated

how to run a bash script

To run a Bash script: make it executable with chmod +x script.sh, then run it with ./script.sh, or skip the chmod and run bash script.sh directly. This page covers execution, permissions, arguments, cron scheduling, and debugging; for how to write the script itself, see how to write a bash script.

You’ll need a Linux, macOS, or other Unix-like shell, and a script file already written (see how to write a Bash script if you don’t have one yet).

Making Your Bash Script Executable

A script needs execute permission before ./script.sh will run it. Here’s the difference, run against the same three-line script:

$ ./demo.sh
-bash: ./demo.sh: Permission denied    # exit 126 — no execute bit set

$ bash demo.sh                          # works without chmod — bash reads and runs the file directly
running, pid=85139

$ chmod +x demo.sh
$ ./demo.sh                             # now works — the kernel executes it via its shebang
running, pid=85142

chmod +x scriptname.sh sets the execute bit for owner, group, and others. Both ./scriptname.sh and bash scriptname.sh run the script in a new child process; a variable the script sets doesn’t leak back into your shell:

$ ./demo.sh
$ echo "myvar in this shell: [$myvar]"
myvar in this shell: []

source scriptname.sh (or . scriptname.sh) is different: it runs the script’s commands in your current shell instead of a child process, so variables it sets do stick around:

$ source demo.sh
$ echo "myvar after sourcing: [$myvar]"
myvar after sourcing: [set-inside-script]

Why ./ is required. Linux and macOS don’t search the current directory for commands unless it’s explicitly in $PATH, which by default it isn’t. Typing the bare filename fails even with execute permission set and the file sitting right there:

$ demo.sh
-bash: demo.sh: command not found     # exit 127

./demo.sh gives an explicit path so the shell doesn’t need to search $PATH at all. To run a script by name from anywhere, either move it into a directory already on $PATH (like /usr/local/bin), or add its directory to $PATH.

Shebang resolution. The #!/bin/bash line at the top of a script is a hardcoded path: it does not consult $PATH at all, even though running the bare word bash in your terminal does. On this Mac that produces two different interpreters depending on how you invoke the same script:

$ cat demo.sh
#!/bin/bash
echo "BASH_VERSION=$BASH_VERSION"

$ ./demo.sh                    # kernel follows the shebang literally: /bin/bash
BASH_VERSION=3.2.57(1)-release

$ bash demo.sh                 # shell looks up 'bash' in $PATH first
BASH_VERSION=5.3.15(1)-release

Apple ships an old GPLv2 /bin/bash (3.2.57) for licensing reasons; a newer Homebrew bash sits earlier in this shell’s $PATH. A script using a bash 4+ feature (associative arrays, for instance) can pass under bash script.sh and fail under ./script.sh on the same machine: they’re not the same interpreter binary.

CRLF line endings. A script edited on Windows and copied over often has \r\n line endings instead of \n. The shebang line becomes #!/bin/bash\r, and the kernel treats the whole thing, including the trailing \r, as one interpreter path:

$ file crlf.sh
crlf.sh: Bourne-Again shell script text executable, ASCII text, with CRLF line terminators

$ ./crlf.sh
-bash: ./crlf.sh: /bin/bash^M: bad interpreter: No such file or directory

That ^M is the carriage return rendered visibly: the interpreter path looked for was /bin/bash\r, which doesn’t exist. Fix with dos2unix scriptname.sh, or sed -i '' 's/\r$//' scriptname.sh on macOS.

how-to-run-a-bash-script.sh asserts the exit codes 126 and 127, the child-process scoping of variables under ./demo.sh versus source, the CRLF bad interpreter failure and the set -e stop from a fresh working directory.

Running Bash Scripts

Direct Execution
Navigate to the script’s directory and run ./scriptname.sh. This requires the execute bit (see above) and a valid shebang.

Using the Bash Command
bash scriptname.sh runs the script without needing the execute bit at all, useful for one-off runs or when you don’t control the file’s permissions.

Scheduling with Cron
For tasks that need to run periodically, schedule the script with cron, a time-based job scheduler. Cron also covers “once, when the machine comes up” (@reboot), but it gives you no dependency ordering, no restart-on-failure and no status command, which is why a systemd unit is the better answer for anything system-level — or, when the wait is just for a slow dependency, a fixed sleep at the top of the script:

  1. Open the crontab with crontab -e.
  2. Add a line specifying the schedule and the script’s absolute path (cron jobs don’t inherit your shell’s working directory or full $PATH), for example 0 5 * * * /path/to/scriptname.sh to run daily at 5 AM.

Passing Arguments to Scripts

Arguments passed on the command line are available inside the script as $1, $2, and so on; $0 is the script’s own name, and $@ or $* expands to all arguments, but only "$@", with the double quotes, survives an argument containing a space, because the other three forms lose argument boundaries:

$ cat args.sh
#!/bin/bash
echo "script name (\$0): $0"
echo "first arg (\$1): $1"
echo "arg count (\$#): $#"

$ ./args.sh alpha beta
script name ($0): ./args.sh
first arg ($1): alpha
arg count ($#): 2

Debugging and Troubleshooting

Common Errors

  • Permission Denied (exit 126): the execute bit isn’t set; run chmod +x scriptname.sh.
  • Command Not Found (exit 127): either a typo in a command name inside the script, or you ran the bare filename without ./ and it’s not on $PATH.
  • Syntax Errors: a text editor with shell syntax highlighting catches most of these before you run the script.

Debug Mode
bash -x scriptname.sh prints each command as it executes, with variables already expanded:

$ bash -x debugme.sh
+ name=World
+ echo 'Hello, World!'
Hello, World!
+ false
+ echo 'This still runs unless set -e is used'
This still runs unless set -e is used

Note that false didn’t stop the script: by default Bash keeps going after a failing command. Add set -e near the top of the script to make it exit immediately on the first command that fails, which is usually what you want for anything beyond a quick one-off:

$ cat strict.sh
#!/bin/bash
set -e
echo "before"
false
echo "after (should not print)"

$ ./strict.sh
before
$ echo "exit: $?"
exit: 1
Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.