The script file and how it runs

Updated November 1, 2026

A script is a text file of commands. To run it as a program you need two things: a shebang on the first line and the execute bit.

The shebang #!/usr/bin/env bash tells the kernel which interpreter to run the file with. env bash finds bash on the PATH, so the script is portable between systems.

chmod +x ~/count.sh   # add the execute bit
./count.sh            # run it as a program
bash ~/count.sh       # run it by calling bash explicitly

Sample output

$ ls -l count.sh
-rw-r--r-- 1 student student 43 Jul 26 03:48 count.sh
$ ./count.sh
bash: ./count.sh: Permission denied
$ chmod +x count.sh
$ ls -l count.sh
-rwxr-xr-x 1 student student 43 Jul 26 03:48 count.sh
$ ./count.sh
hello from script

./count.sh only works with the x bit. bash count.sh always works: there you hand the file to the interpreter yourself, so the bit is not needed.

Common mistakes

Two similar refusals mean different things. bash: ./count.sh: Permission denied (exit 126) means the file was found but has no x bit; chmod +x fixes it. bash: count.sh: command not found (exit 127) means you left out the ./, so bash searched PATH, which does not include the current directory. The first is about permissions, the second about the path.

A script saved on Windows dies with /usr/bin/env: ‘bash\r’: No such file or directory. A carriage return is stuck at the end of the first line, so the kernel looks for an interpreter literally named bash\r. The message reads as nonsense until you spot that \r. One command fixes it: sed -i 's/\r$//' script.sh.

A typo in the shebang gives bash: ./bad.sh: cannot execute: required file not found. The message names your script, but the missing thing is the interpreter: there is no such file as the #!/bin/basj in the first line. Read line one of the file instead of hunting for what is wrong with the script itself.

Used in

You need this in the lesson Bash: writing scripts.