Script Permissions and Running
Before you can run a script directly, it needs the correct permissions. This lesson covers how to make scripts executable and best practices for script permissions.
Understanding Execute Permission
In Linux, files have three types of permissions:
- Read (r): View file contents
- Write (w): Modify file contents
- Execute (x): Run as a program
For scripts, you need execute permission to run them directly.
Checking Current Permissions
Use ls -l to see file permissions:
ls -l script.sh
# -rw-r--r-- 1 user group 45 Jan 10 10:00 script.sh
# ↑ No execute permission (no 'x')
Making Scripts Executable
Use chmod to add execute permission:
# Add execute for owner
chmod u+x script.sh
# Add execute for everyone
chmod +x script.sh
# Using octal notation
chmod 755 script.sh
Understanding chmod Options
| Command | Meaning |
|---|---|
chmod +x | Add execute for all |
chmod u+x | Add execute for user (owner) |
chmod g+x | Add execute for group |
chmod o+x | Add execute for others |
chmod 755 | rwxr-xr-x (common for scripts) |
chmod 700 | rwx------ (private scripts) |
Exercise: Make Script Executable
Create a script and make it executable:
Running Scripts from Different Locations
The way you call a script depends on where it is:
# Script in current directory
./script.sh
# Script with full path
/home/user/scripts/script.sh
# Script in PATH
script.sh # Only works if directory is in PATH
Why ./ is Required
When you type a command, bash searches the PATH:
echo $PATH
# /usr/local/bin:/usr/bin:/bin
# 'ls' works because it's in /usr/bin (which is in PATH)
ls
# './script.sh' is needed because . is NOT in PATH
./script.sh
Adding Scripts to PATH
To run scripts without ./, add their directory to PATH:
# Temporarily add ~/bin to PATH
export PATH="$HOME/bin:$PATH"
# Now scripts in ~/bin can be run directly
myscript.sh # Instead of ~/bin/myscript.sh
For permanent changes, add to ~/.bashrc:
# In ~/.bashrc
export PATH="$HOME/bin:$PATH"
Common Permission Patterns
| Octal | Symbolic | Use Case |
|---|---|---|
| 755 | rwxr-xr-x | Shared scripts (most common) |
| 700 | rwx------ | Private scripts |
| 750 | rwxr-x--- | Scripts for your group only |
| 644 | rw-r--r-- | Non-executable files |
Script Ownership
Sometimes you need to consider ownership:
# Check ownership
ls -l script.sh
# -rwxr-xr-x 1 alice developers 100 Jan 10 script.sh
# ↑ ↑
# owner group
# Change owner (requires sudo)
sudo chown bob script.sh
# Change group
chown :newgroup script.sh
Security Best Practices
Follow these security guidelines:
- Don't use 777: Never
chmod 777- anyone can modify and run - Limit access: Use 700 for sensitive scripts
- Check scripts before running: Especially downloaded scripts
- Use appropriate owners: Scripts in
/usr/local/binshould be owned by root
# BAD - anyone can modify
chmod 777 script.sh
# GOOD - only owner can modify, others can execute
chmod 755 script.sh
# GOOD - private script
chmod 700 private_script.sh
Troubleshooting Permission Issues
Common errors and solutions:
# Error: Permission denied
$ ./script.sh
bash: ./script.sh: Permission denied
# Solution: chmod +x script.sh
# Error: Command not found
$ script.sh
bash: script.sh: command not found
# Solution: Use ./script.sh or add to PATH
# Error: Bad interpreter
$ ./script.sh
bash: ./script.sh: /bin/bash^M: bad interpreter
# Solution: File has Windows line endings, use dos2unix
Exercise: Check and Set Permissions
View permissions and understand what they mean:
Quick Reference
# Create and run a script - complete workflow
touch myscript.sh # Create file
echo '#!/bin/bash' > myscript.sh # Add shebang
echo 'echo "It works!"' >> myscript.sh # Add commands
chmod +x myscript.sh # Make executable
./myscript.sh # Run it
Key Takeaways
- Scripts need execute permission (
chmod +x) to run directly - Use
./script.shto run scripts in the current directory chmod 755is the standard permission for shared scripts- Add script directories to PATH for convenient access
- Never use
chmod 777- it's a security risk - Use
chmod 700for private, sensitive scripts

