🧠 5. Raspberry Pi – Programming & Scripting
Estimated reading: 3 minutes 281 views

Raspberry Pi – Shell Scripting Basics (2025 Beginner’s Automation Guide)


Introduction – Automate Tasks with Shell Scripts on Raspberry Pi

Shell scripting allows you to automate repetitive tasks, control hardware, manage files, or monitor your Raspberry Piβ€”all through the command line. It’s a vital skill for Raspberry Pi users building IoT, Linux automation, or embedded projects.

In this guide, you’ll learn:

  • How to write, run, and schedule shell scripts on Raspberry Pi
  • Bash syntax essentials: variables, loops, conditionals
  • Real-world Raspberry Pi automation examples
  • Best practices for script organization and debugging

What Is a Shell Script?

A shell script is a text file containing a series of shell (Bash) commands that the Raspberry Pi executes sequentially.

Create a script:

nano myscript.sh

Add content:

#!/bin/bash
echo "Hello, Raspberry Pi!"

Make it executable:

chmod +x myscript.sh

Run the script:

./myscript.sh

Output:

Hello, Raspberry Pi!

Basic Syntax and Constructs

Variables:

name="Raspberry"
echo "Hello $name Pi!"

Conditionals:

if [ $temp -gt 40 ]; then
  echo "Overheating!"
fi

Loops:

for i in {1..5}
do
  echo "Blink $i"
done

User Input:

read -p "Enter your name: " user
echo "Welcome $user"

Example: LED Control with Shell Script + GPIO

Script: led_on.sh

#!/bin/bash
gpio -g mode 17 out
gpio -g write 17 1
echo "LED ON"

Script: led_off.sh

#!/bin/bash
gpio -g write 17 0
echo "LED OFF"

These control an LED connected to GPIO17 using the gpio CLI tool (install with sudo apt install wiringpi or gpio-utils depending on OS version).


Example: System Monitor Script

Script: sysinfo.sh

#!/bin/bash
echo "CPU Temp:"
vcgencmd measure_temp

echo "Disk Usage:"
df -h

echo "Memory Usage:"
free -h

Automates hardware health check for Pi.


Automate with Cron (Task Scheduler)

Edit crontab:

crontab -e

Run a script every hour:

0 * * * * /home/pi/scripts/sysinfo.sh >> /home/pi/logs/status.log

Great for logging data or automating GPIO controls.


Tips for Writing Effective Shell Scripts

Best Practice Avoid
Use #!/bin/bash at the topOmitting shebang (won’t execute)
Test commands individuallyWriting without checking output
Add comments (#)Unclear logic and flow
Use meaningful variable namesUsing generic names like x, y
Always handle permissionsRunning scripts blindly as root

Real-World Raspberry Pi Shell Script Projects

Project Name Description
Auto Wi-Fi ReconnectScript to reconnect dropped Wi-Fi signal
Camera TimelapseCron + libcamera to capture every minute
LED Blinking AutomationShell loop with gpio commands
Safe Shutdown ButtonGPIO input triggers shutdown via script
Pi Self-Healing ScriptMonitor and restart failed processes

Summary – Recap & Next Steps

Shell scripting is a core tool for controlling, automating, and managing your Raspberry Pi through Linux. It enables fast prototyping and hands-free control of hardware and system services.

Key takeaways:

  • Shell scripts are sequences of Bash commands with .sh extensions
  • You can automate hardware (GPIO), system checks, and user interactions
  • Cron allows scheduling scripts periodically
  • Shell scripting is essential for headless Pi projects and automation tasks

Real-world relevance: Used in headless servers, startup routines, camera control, IoT sensors, and custom command-line utilities.


FAQs – Raspberry Pi Shell Scripting

Do I need to install anything to use shell scripts?

No. Raspberry Pi OS comes with Bash and all shell scripting basics pre-installed.


Can I use shell scripts to control GPIO?

Yes. Use the gpio command-line tool (wiringpi or gpio-utils) or call Python scripts from shell.


How do I debug a shell script?

Add set -x at the top or run with:

bash -x script.sh

Can I trigger a shell script on boot?

Yes. Add it to rc.local, ~/.bashrc, or a systemd service file.


What is the best way to log script output?

Use redirection:

./myscript.sh >> log.txt 2>&1

Share Now :
Share

πŸ–₯️ Raspberry Pi – Shell Scripting Basics

Or Copy Link

CONTENTS
Scroll to Top