Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 modes available
Concept Flow - Making scripts executable (chmod +x)
Write script file
↓
Check file permissions
↓
Run: permission denied?
Yes→Use 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.

Practice

(1/5)
1. What does the command chmod +x script.sh do in bash scripting?
easy
A. Makes the script script.sh executable so it can be run directly.
B. Deletes the script script.sh from the system.
C. Copies the script script.sh to another location.
D. Changes the owner of the script script.sh.

Solution

  1. Step 1: Understand the chmod command

    The chmod command changes file permissions in Linux and Unix systems.
  2. Step 2: Interpret the +x option

    The +x option adds execute permission, allowing the file to be run as a program.
  3. Final Answer:

    Makes the script script.sh executable so it can be run directly. -> Option A
  4. Quick Check:

    chmod +x means add execute permission [OK]
Hint: Remember +x means add execute permission [OK]
Common Mistakes:
  • Confusing execute permission with deleting files
  • Thinking chmod copies files
  • Mixing up ownership change with permission change
2. Which of the following is the correct syntax to make a script named run.sh executable?
easy
A. chmod x run.sh
B. chmod x+ run.sh
C. chmod -x run.sh
D. chmod +x run.sh

Solution

  1. Step 1: Recall the correct order of options in chmod

    The correct syntax to add execute permission is chmod +x filename.
  2. Step 2: Check each option

    chmod +x run.sh matches the correct syntax exactly; others have incorrect order or missing symbols.
  3. Final Answer:

    chmod +x run.sh -> Option D
  4. Quick Check:

    Correct syntax is chmod +x filename [OK]
Hint: Use chmod +x filename to add execute permission [OK]
Common Mistakes:
  • Placing + and x in wrong order
  • Using -x which removes execute permission
  • Omitting the + sign
3. Given a script hello.sh without execute permission, what will be the output of the command sequence below?
ls -l hello.sh
chmod +x hello.sh
ls -l hello.sh
medium
A. Both ls -l commands show execute permission.
B. The first ls -l shows no execute permission; the second shows execute permission added.
C. The first ls -l shows execute permission; the second removes it.
D. Both ls -l commands show no execute permission.

Solution

  1. Step 1: Understand initial permissions

    Initially, hello.sh has no execute permission, so ls -l shows no 'x' in permissions.
  2. Step 2: Apply chmod +x and check again

    After running chmod +x hello.sh, execute permission is added, so the second ls -l shows 'x' in permissions.
  3. Final Answer:

    The first ls -l shows no execute permission; the second shows execute permission added. -> Option B
  4. Quick Check:

    Adding +x updates permissions visible in ls -l [OK]
Hint: Check permissions before and after with ls -l [OK]
Common Mistakes:
  • Assuming execute permission is present initially
  • Thinking chmod +x removes execute permission
  • Not noticing permission changes in ls -l
4. You try to run a script myscript.sh but get a "Permission denied" error. Which command fixes this issue?
medium
A. chmod -x myscript.sh
B. chmod +r myscript.sh
C. chmod +x myscript.sh
D. chmod 000 myscript.sh

Solution

  1. Step 1: Identify cause of "Permission denied"

    The error usually means the script lacks execute permission.
  2. Step 2: Add execute permission

    Using chmod +x myscript.sh adds execute rights, allowing the script to run.
  3. Final Answer:

    chmod +x myscript.sh -> Option C
  4. Quick Check:

    Add execute permission to fix "Permission denied" [OK]
Hint: Add execute permission with chmod +x [OK]
Common Mistakes:
  • Using -x which removes execute permission
  • Setting permissions to 000 which denies all access
  • Adding only read permission (+r) which doesn't allow execution
5. You have a script deploy.sh that you want to run directly by typing ./deploy.sh. However, it still says "Permission denied" after chmod +x deploy.sh. What could be the reason?
hard
A. The script is on a mounted filesystem that does not support execute permissions.
B. You forgot to add read permission with chmod +r deploy.sh.
C. You need to rename the script to deploy.exe.
D. You must run the script with bash deploy.sh instead of ./deploy.sh.

Solution

  1. Step 1: Confirm execute permission is set

    Since chmod +x deploy.sh was run, the script should have execute permission.
  2. Step 2: Consider filesystem limitations

    Some mounted filesystems (like FAT32 or network shares) do not support Unix execute permissions, causing "Permission denied" despite chmod +x.
  3. Step 3: Evaluate other options

    Adding read permission is usually default; renaming to .exe is irrelevant in Linux; running with bash works but does not explain the permission error when running ./deploy.sh.
  4. Final Answer:

    The script is on a mounted filesystem that does not support execute permissions. -> Option A
  5. Quick Check:

    Filesystem may block execute permission despite chmod [OK]
Hint: Check if filesystem supports execute permissions [OK]
Common Mistakes:
  • Assuming chmod always works on all filesystems
  • Thinking read permission alone allows execution
  • Confusing script name extension with execution ability
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard