0
0
Bash Scriptingscripting~10 mins

Making scripts executable (chmod +x) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Making scripts executable (chmod +x)
Write script file
Check file permissions
Run: permission denied?
YesUse chmod +x to add execute permission
File now executable
Run script successfully
This flow shows how a script file is made executable by adding execute permission with chmod +x, allowing it to run without permission errors.
Execution Sample
Bash Scripting
#!/bin/bash
# hello.sh
echo "Hello, world!"
This script prints 'Hello, world!'. We add execute permission with chmod +x, then run it.
Execution Table
StepCommandFile Permission BeforeActionFile Permission AfterOutput
1ls -l hello.sh-rw-r--r--Check permissions-rw-r--r--
2./hello.sh-rw-r--r--Try to run script-rw-r--r--bash: ./hello.sh: Permission denied
3chmod +x hello.sh-rw-r--r--Add execute permission-rwxr-xr-x
4ls -l hello.sh-rwxr-xr-xVerify permissions-rwxr-xr-x
5./hello.sh-rwxr-xr-xRun script-rwxr-xr-xHello, world!
💡 Execution stops after successful script run with execute permission.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5
File PermissionN/A-rw-r--r---rwxr-xr-x-rwxr-xr-x
Script OutputN/AN/AN/AHello, world!
Key Moments - 3 Insights
Why does the script give 'Permission denied' before using chmod +x?
Because the file lacks execute permission (-rw-r--r--), so the system blocks running it. See execution_table step 2.
What does chmod +x actually do to the file permissions?
It adds execute permission for user, group, and others, changing -rw-r--r-- to -rwxr-xr-x as shown in step 3.
Why can we run the script after chmod +x but not before?
Because only after adding execute permission does the system allow running the file as a program, shown by successful output in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the file permission after step 1?
A-rw-r--r--
B-rwxr-xr-x
C-r--r--r--
D-rw-rw-rw-
💡 Hint
Check the 'File Permission After' column in step 1.
At which step does the script become executable?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'File Permission After' columns to see when execute permission is added.
If we skip chmod +x, what output do we get when running ./hello.sh?
AHello, world!
BPermission denied
CNo such file or directory
DSyntax error
💡 Hint
See step 2 output in the execution table.
Concept Snapshot
chmod +x filename
- Adds execute permission to a script file
- Changes permissions from non-executable to executable
- Allows running script with ./filename
- Without +x, running script gives 'Permission denied'
- Check permissions with ls -l
Full Transcript
This lesson shows how to make a script file executable using chmod +x. Initially, the script file has no execute permission, so trying to run it results in 'Permission denied'. Using chmod +x adds execute permission, changing the file mode to allow running it. After this, running the script prints the expected output. The execution table traces each step: checking permissions, trying to run, adding execute permission, verifying permissions, and running successfully. Key moments clarify why permission denied happens and what chmod +x does. The quiz tests understanding of permission states and effects of chmod +x.