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 ~/scriptsAlternative naming: Many users prefer ~/bin for their personal scripts, which is also a common convention.
Navigate to Your Scripts Directory
cd ~/scriptsCreating Your First Script
Using a Text Editor
Create your script file using any text editor (vim, nano, gedit, etc.):
vim first_script.shImportant 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 directorymkdir -p drun
# Create a file with contentecho "hello bash world" > drun/file.txt
# List current directory contentsls -la .
# Display the file contentscat drun/file.txtExplanation:
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.shOption 2: All Users
chmod +x first_script.shRunning Your Script
Method 1: Using Relative Path (Current Directory)
./first_script.shThe ./ tells the shell to look in the current directory.
Method 2: Using Absolute Path (From Any Directory)
/home/student/scripts/first_script.shMethod 3: Just the Script Name (After PATH Configuration)
Once you configure your PATH (explained below), you can simply run:
first_script.shUnderstanding 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 $PATHYou’ll see something like:
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/gamesWhy 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:~/scriptsPermanent Addition (Persists After Reboot)
-
Open your
.bashrcfile:Terminal window nano ~/.bashrc -
Add this line at the end:
Terminal window export PATH="$PATH:~/scripts" -
Save and exit (Ctrl+X, then Y, then Enter in nano)
-
Reload the configuration:
Terminal window source ~/.bashrcOr simply open a new terminal.
Verify the Change
echo $PATHYou 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!