← Operating System Basics: From Zero to Hero
Level 3 · Hero: Power Moves

Kernel, Shell and Your First Script

Lesson 12 of 14 · ⏱ 14 min

You've used the terminal a lot. Now let's see how Linux is built, in layers like an onion, and then become a real power user. 🧅

Concentric circles: hardware in the centre, the kernel, then the shell, then apps and you. A GUI lets you click, a CLI lets you type commands.

  • Kernel ❤️: the heart. It runs processes, shares memory and talks to hardware.
  • Shell 🐚: the layer that listens to you. It reads your commands and asks the kernel to do them.
  • Apps 📱: the programs you use.

A GUI (graphical interface) uses windows and icons. A CLI (command line interface) uses typed commands: fast, precise and powerful for experts.

🧪 Lab 1: find the kernel and the shell

uname -r
echo $SHELL
ps -p $$ -o comm=
  • The kernel is the thing uname talks about.
  • The shell is your bash (it appears from echo $SHELL and ps -p $$).
type ls
which ls

which tells you where a command's program lives. type also shows if it's built into the shell. Shell commands are just programs the shell starts for you!

🧪 Lab 2: ask for help

man ls

Press Space to scroll, and q to leave. Every command has a manual page. Try a quicker help too:

ls --help | head -15

🧪 Lab 3: build a pipeline with | and >

You've seen | and > in earlier labs. Now let's understand them. Linux has a wonderful idea: small tools that do one job each, joined together like LEGO.

A pipeline: the output of ps aux flows through a pipe to head, and a redirect sends it into a file.

  • | (the pipe) sends the output of one command into the next.
  • > writes output into a file (and replaces it). >> adds to the end.

Try it:

cd ~/MagiLab
ps aux | head -5
ps aux | head -5 > processes.txt
cat processes.txt
ls /usr/bin | wc -l
ls /usr/bin | sort | head -5
history | tail -5
  • sort puts lines in order, wc -l counts them, and history shows your earlier commands.

🧱 The Linux secret: you can combine small commands into powerful ones, like ls /usr/bin | wc -l.

🧪 Lab 4: variables and the environment

echo $HOME
echo $USER
echo $PATH

These are environment variables: little notes the shell keeps for programs. PATH is a list of folders where the shell hunts for commands. That's how it found ls!

NAME="Magi"
echo "Hello, $NAME!"

🧪 Lab 5: write your first script

A shell script is a text file full of commands. Let's make one.

cd ~/MagiLab
nano hello.sh

(If nano isn't installed, use echo lines to create the file instead, or ask a grown-up to install it.) Type this into the editor:

#!/bin/bash
echo "Hello from my first Linux script!"
echo "I am $(whoami) on a computer called $(hostname)."
date

Save with Ctrl + O, press Enter, and exit with Ctrl + X.

  • The first line, #!/bin/bash, tells the OS which program should run the script.

Now let's make the script executable and run it:

chmod +x hello.sh
./hello.sh
  • chmod +x gives the file permission to run (we'll learn about permissions next).
  • ./hello.sh means "run the program called hello.sh in this folder".

🎉 You just wrote a program, and the shell asked the kernel to run it as a new process!

Words to know

Word What it means
Shell The program that reads your commands
**Pipe (` `)**
Script A text file full of commands
Environment variable A named note the shell shares with programs

🎯 Quiz time!

1. What does the shell do?
2. What does the | symbol do?
3. What is the first line #!/bin/bash in a script for?
4. What does chmod +x hello.sh do?