AI Hints
Home›Linux Tutorial›Running and Owning
Linux · Lesson 67 of 120

Running a Script

Chmod +x and ./.

⏱ 2 min read▶ Video at 1:27:55🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

A script is a text file full of commands. You'll write lots of them later in this course. For now, let's see what it takes to actually run one, because permissions play a big part.

Running a Script
From the video: Running a Script
  • A script: a file of commands
  • More on them later
  • How to run one

Not allowed, yet

We go into the scripts folder. To run a program from the current folder, you type dot slash, and its name. But we get permission denied. The file isn't executable yet. There's no X in its permissions.

Terminal
cd projects/scripts
./hello.sh
bash: ./hello.sh: Permission denied
ls -l hello.sh
-rw-r--r-- 1 sam sam 40 Jan  5 09:00 hello.sh

Making it executable

CH mod plus X gives execute permission to everyone. When you leave out the who, it applies to all. Now dot slash hello dot SH runs, and prints its message. That's all it takes to turn a text file into a program.

Terminal
chmod +x hello.sh
ls -l hello.sh
-rwxr-xr-x 1 sam sam 40 Jan  5 09:00 hello.sh
./hello.sh
Hello from my script

Why dot slash?

Why the dot slash? When you type a plain name, like LS, the shell only looks in a list of program folders. Your current folder isn't on that list, for safety. Dot slash means, run the file in this folder, right here. We'll learn about that list of folders, called the path, later.

keyvalue
lsfound in program folders
hello.shnot found: not in the list
./hello.shrun it from here
Safetythe current folder isn't searched

Summary

To run a script, make it executable with CH mod plus X, then run it with dot slash and its name. Next, file owners, and how to change them.

Key takeaways
  • chmod +x script
  • ./script
  • No x: permission denied
Next up: Ownership with chown — who owns a file