Creating Your First Shell Script and Understanding PATH
Letβs dive into hands-on Bash scripting by creating your very first script! This practical guide will walk you through the entire process from creation to execution, plus the crucial concept of the PATH variable.
Setting Up Your Workspace
Creating a Scripts Directory
First, create a dedicated directory for your scripts in your home directory:
mkdir ~/scripts
Alternative naming: Many users prefer ~/bin for their personal scripts, which is also a common convention.
Navigate to Your Scripts Directory
cd ~/scripts
Creating Your First Script
Using a Text Editor
Create your script file using any text editor (vim, nano, gedit, etc.):
vim first_script.sh
Important Notes on Filenames:
- Avoid spaces in filenames - use underscores instead
- Extension convention:
.shextension is common but optional - Linux tradition: Extensions arenβt strictly necessary for executables
Writing the Script Content
Enter these commands exactly as you would type them in the terminal:
#!/bin/bash
# Create a new directory
mkdir -p drun
# Create a file with content
echo "hello bash world" > drun/file.txt
# List current directory contents
ls -la .
# Display the file contents
cat drun/file.txt
Explanation:
mkdir -p drun: Creates directorydrun(with-pto avoid errors if it exists)echo "hello bash world" > drun/file.txt: Writes text to a filels -la .: Lists current directory detailscat drun/file.txt: Displays file contents
Making Your Script Executable
Before you can run your script, you need to give it execute permissions:
Option 1: Owner Only (Recommended for personal scripts)
chmod 700 first_script.sh
Option 2: All Users
chmod +x first_script.sh
Running Your Script
Method 1: Using Relative Path (Current Directory)
./first_script.sh
The ./ tells the shell to look in the current directory.
Method 2: Using Absolute Path (From Any Directory)
/home/student/scripts/first_script.sh
Method 3: Just the Script Name (After PATH Configuration)
Once you configure your PATH (explained below), you can simply run:
first_script.sh
Understanding the PATH Variable
What is PATH?
The PATH variable is an environment variable that tells the shell where to look for executable files. When you type a command, the shell searches through directories listed in PATH in order.
Viewing Your Current PATH
echo $PATH
Youβll see something like:
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Why Running Just the Script Name Fails Initially
When you type first_script.sh, the shell searches for it in the PATH directories. Since your ~/scripts directory isnβt in PATH, you get βcommand not found.β
Adding Your Scripts Directory to PATH
Temporary Addition (Current Session Only)
export PATH=$PATH:~/scripts
Permanent Addition (Persists After Reboot)
-
Open your
.bashrcfile:nano ~/.bashrc -
Add this line at the end:
export PATH="$PATH:~/scripts" -
Save and exit (Ctrl+X, then Y, then Enter in nano)
-
Reload the configuration:
source ~/.bashrcOr simply open a new terminal.
Verify the Change
echo $PATH
You should now see ~/scripts at the end of the PATH.
Important Security Consideration
PATH Order Matters
The shell searches directories in the order they appear in PATH. If you have a script named ls in your scripts directory, it wonβt override the systemβs ls command because /bin appears before ~/scripts in the PATH.
Best Practice: Avoid naming your scripts the same as common system commands to prevent confusion.
Troubleshooting Common Issues
βCommand Not Foundβ Errors
- Check if script is executable:
ls -la first_script.sh - Verify PATH includes script directory:
echo $PATH - Reload .bashrc:
source ~/.bashrc
Permission Denied
- Add execute permission:
chmod +x first_script.sh
Script Runs But Commands Fail
- Check command syntax: Ensure commands work when typed directly in terminal
- Verify file paths: Use absolute paths or relative paths correctly
Key Takeaways
- Scripts are just text files containing commands youβd normally type
- Permissions are required: Scripts need execute permission (
chmod +x) - PATH determines accessibility: The shell only finds commands in PATH directories
- Two execution methods: Relative path (
./script.sh) or absolute path (/full/path/script.sh) - PATH modifications: Can be temporary (current session) or permanent (via
.bashrc) - Security matters: Be mindful of PATH order and script naming
Next Steps
Now that youβve created your first script, you can:
- Add more complex logic (conditionals, loops, functions)
- Create aliases for your most-used scripts
- Schedule scripts to run automatically with cron
- Share scripts with other users
Your journey into Bash automation has officially begun!