How to Write Basic Shell Scripts on Arch Linux
Categories:
5 minute read
Shell scripting is an essential skill for any Linux user, particularly those who use a lightweight and hands-on distribution like Arch Linux. Whether you’re automating mundane tasks, managing system maintenance, or chaining together complex command sequences, shell scripts offer a powerful way to increase your productivity and efficiency.
In this article, we’ll walk through the fundamentals of writing basic shell scripts on Arch Linux. We’ll cover what shell scripts are, how to create and run them, and provide practical examples to get you started.
What is a Shell Script?
A shell script is a plain text file containing a series of shell commands. These commands are executed sequentially by the shell interpreter, typically Bash (Bourne Again SHell), which is the default on Arch Linux. However, other shells like zsh
, fish
, or sh
can also be used.
Scripts can be as simple as a few lines automating a daily task, or complex programs with logic, loops, and function definitions.
Prerequisites
Before we start writing shell scripts, ensure you have the following:
1. A working Arch Linux system
Make sure your system is updated:
sudo pacman -Syu
2. A text editor
You can use any terminal-based or GUI editor. Common ones include:
nano
(easy to use)vim
orneovim
(powerful, steeper learning curve)gedit
,mousepad
, orkate
(GUI editors)
Example installation:
sudo pacman -S nano
Creating Your First Shell Script
Step 1: Create a file
Open your terminal and create a new file:
nano hello.sh
Step 2: Add the shebang
At the top of the file, include the shebang to indicate which shell will interpret the script:
#!/bin/bash
This tells the system to use the Bash shell to execute the script.
Step 3: Add your commands
Below the shebang, add a simple command:
#!/bin/bash
echo "Hello, Arch Linux user!"
Step 4: Save and exit
- If using
nano
, pressCtrl + O
, thenEnter
to save, andCtrl + X
to exit.
Making the Script Executable
You need to give execute permission to the script:
chmod +x hello.sh
Now you can run it like this:
./hello.sh
Or, if the file is not in your $PATH
:
bash hello.sh
Basic Shell Script Concepts
Let’s explore essential scripting concepts with examples.
1. Variables
You can store values in variables for reuse:
#!/bin/bash
name="ArchUser"
echo "Hello, $name!"
No spaces around =
when assigning values. To access the variable, use $name
.
2. User Input
Use read
to accept input:
#!/bin/bash
echo "What is your name?"
read username
echo "Hello, $username!"
3. Conditional Statements
Use if
statements to perform logic:
#!/bin/bash
echo "Enter a number:"
read number
if [ "$number" -gt 10 ]; then
echo "The number is greater than 10"
else
echo "The number is 10 or less"
fi
4. Loops
Loops allow repetitive tasks.
For Loop
#!/bin/bash
for i in {1..5}
do
echo "Loop $i"
done
While Loop
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
((count++))
done
5. Functions
Functions are reusable blocks of code.
#!/bin/bash
greet_user() {
echo "Hello, $1!"
}
greet_user "ArchUser"
Here, $1
is the first argument passed to the function.
Real-World Examples
Let’s look at a few practical scripts.
Example 1: Backup a Directory
#!/bin/bash
src="$HOME/Documents"
dest="$HOME/backup_$(date +%Y%m%d_%H%M%S).tar.gz"
tar -czf "$dest" "$src"
echo "Backup created at $dest"
This script compresses your Documents folder into a timestamped .tar.gz
file.
Example 2: Update System
#!/bin/bash
echo "Updating system..."
sudo pacman -Syu
echo "System update completed."
Useful for automating your Arch system updates.
Example 3: Check if a Package is Installed
#!/bin/bash
echo "Enter package name:"
read pkg
if pacman -Qi "$pkg" > /dev/null 2>&1; then
echo "$pkg is installed."
else
echo "$pkg is NOT installed."
fi
Best Practices for Shell Scripting
Use comments to describe what your script is doing:
# This script updates the system
Validate inputs:
if [ -z "$1" ]; then echo "Usage: $0 <filename>" exit 1 fi
Quote variables to avoid word splitting:
echo "$filename"
Use meaningful variable names.
Test scripts in a safe environment before applying them to critical systems.
Debugging Your Script
Use bash -x script.sh
to run in debug mode. This will show you each command as it executes, which is helpful for troubleshooting.
Using Shellcheck for Linting
Install shellcheck
to analyze your scripts for potential issues:
sudo pacman -S shellcheck
Run it like this:
shellcheck yourscript.sh
It will highlight syntax errors, bad practices, and suggest improvements.
Making Scripts Portable
If you want your script to work on other distributions:
- Use
#!/bin/sh
for POSIX-compliant scripting. - Avoid Bash-specific features unless you are certain the system uses Bash.
- Test on multiple systems when portability matters.
Automating Script Execution
To run scripts automatically, consider adding them to crontab
(for scheduled jobs) or .bashrc
(to run at login).
Example: Add to Crontab
crontab -e
Add a line:
0 3 * * * /home/user/backup.sh
This runs the script daily at 3 AM.
Conclusion
Writing shell scripts on Arch Linux is both educational and empowering. With just a little practice, you can begin automating tasks, managing your system more efficiently, and developing deeper insight into how Linux works. The minimalist nature of Arch encourages this type of hands-on learning and customization, making it a great platform for scripting enthusiasts.
As you grow more comfortable, you can begin to explore more advanced scripting topics like:
- Argument parsing (
getopts
) - Using external tools (e.g.,
awk
,sed
,jq
) - Writing interactive menus (
select
,dialog
) - Integrating with systemd for service management
Keep experimenting, and soon you’ll find shell scripting to be one of the most valuable tools in your Linux toolbox.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.