๐ฏ Objective
Allow a script to be executed as a program by setting the correct execution permission.
In Linux, a file must have execute (x) permission before it can run as a script.
๐ What is script execution permission?
Linux uses file permissions to control who can:
Read (r) โ view the file
Write (w) โ modify the file
Execute (x) โ run the file as a program
Permissions are defined for three categories:
User (u) โ file owner
Group (g) โ users in the same group
Others (o) โ all other users
Without execute permission, the script cannot be run directly (you will see an error like Permission denied).
๐ ๏ธ Step 1: Create a script
bash
nano script.shAdd the following content:
bash
#!/bin/bash
echo "Hello DevOps"Save and exit.
๐ Step 2: Check current permissions
bash
ls -l script.shExample output:
bash
-rw-r--r-- 1 user user 30 Mar 4 script.shThis shows there is no execute permission yet.
โ๏ธ Step 3: Add execute permission
bash
chmod +x script.shCheck again:
bash
ls -l script.shExample output:
bash
-rwxr-xr-x 1 user user 30 Mar 4 script.shNow the script is executable.
๐ง Understanding chmod +x
bash
chmod +x script.shAdds execute permission for all users. It is equivalent to:
bash
chmod a+x script.shWhere a means u + g + o.
โถ๏ธ Step 4: Run the script
bash
./script.shOutput:
bash
Hello DevOpsโ ๏ธ Important notes
chmod +x makes the script executable.
./script.sh runs the script from the current directory.
Avoid using chmod 777 unless absolutely necessary.
Follow the least privilege principle.