Shebang, chmod +x & ./script

Run your own script

Lesson 16 of 20 · 11 min · Module 5: Permissions and the system

A shell script is a text file full of commands. Once it is executable, you can run the whole sequence with one word.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Write a script file
  • Make a file executable
  • Run a script from a folder

The idea

The first line #!/bin/bash (the shebang) says which program runs the script. chmod +x file adds execute permission. Run a script in the current folder with ./name.sh: the ./ says "this folder", because the shell does not search there by itself.

echo '#!/bin/bash' > hi.sh
echo 'echo "Hi!"' >> hi.sh
chmod +x hi.sh
./hi.sh

01Single quotes keep text exact

Inside single quotes, the shell changes nothing: no variables, no special characters. That is how you write a line containing double quotes into a file.

02Execute permission is required

Without x, running ./bin/greet.sh fails with "Permission denied", even though the file exists. chmod +x adds execute for everyone; chmod u+x adds it only for you.

./projects/scripts/backup.sh
Output
bash: ./projects/scripts/backup.sh: Permission denied

03Why ./ is needed

The shell looks for commands in the folders listed in $PATH, not in the current folder. ./greet.sh or bin/greet.sh gives an explicit path, so the shell knows exactly what to run.

Your turn

Create bin/greet.sh in your home folder. Its first line must be #!/bin/bash and its second line echo "Hello from my first script". Make it executable, then run it.

What the checks look for

  • The script starts with the shebang
  • The script contains the echo line
  • The script is executable
  • Running it prints the greeting
Try it in the editor →

Take this with you

A script is a file of commands: add a shebang, add execute permission, run it by path.

Go a little further

Add a third line to the script that prints the date with date, then run it again.

Learn Linux Command Line for free

20 short lessons with real code and instant checks.

See the whole course →