Linux & Bash Reference

Understanding Linux and Bash

The Kernel

The Kernel is the brain of the OS. It manages memory, CPU processes, and hardware. Linus Torvalds created the Linux kernel based on UNIX principles.

Shell & Terminal

The Shell interprets your text commands for the OS (e.g., Bash, Zsh, PowerShell).

The Terminal is the window or app that gives you access to the shell.

Unix Basic Commands

The essential commands every Linux user needs from day one. Linux is case-sensitive — always check your capitalisation.

§ 01
admin@kasykey:~$
The prompt. ~ represents the Home Directory.
ls
Lists the contents of the current folder.
cat hello.txt
Reads and prints the content of a file (Concatenate).
cd Desktop
Change Directory. Moves you into the 'Desktop' folder. Note: Linux is case-sensitive!
cd ..
Moves "up" one level to the parent directory.

Installing Programs

Use sudo apt to install software. The pipe | lets you chain commands together powerfully.

§ 02
sudo apt search password
Searches the repository for packages related to "password". sudo grants Administrator permissions.
sudo apt install cowsay
Installs the "cowsay" program.
figlet hello | cowsay -n
The Pipe (|): Takes the output of figlet and feeds it as input into cowsay.

Redirection Operators

Control where command input comes from and where output goes. Fundamental for scripting and automation.

§ 03
OperatorNameDescription
< Input Redirect Takes data from a file and feeds it into a command.
> Output Redirect Redirects output, overwriting the target file.
>> Append Redirect Redirects output, appending to the file (existing content preserved).
redirection.sh
echo hello > file.txt # sends "hello" into file.txt, overwriting it echo world >> file.txt # appends "world" — does NOT overwrite cat < file.txt # reads file.txt and prints to terminal
Tip: Combine redirection with pipes for powerful one-liners. Example: cat names.txt | sort > sorted.txt

Folder and File Management

Create, copy, move, and delete files and directories from the command line.

§ 04
mkdir new_folder/sub_folder
Make Directory. Creates a new folder or nested folders.
cp file.txt ~/Desktop
Copy. Copies file.txt to the Desktop directory (~ = home folder).
mv old_name.txt new_name.txt
Move / Rename. Changes the file's location or name.
rmdir empty_folder
Remove Directory. Only works if the folder is empty.
rm file.txt
Remove File. Deletes immediately — no undo! Caution

Apt Package Manager Details

The Aptitude package manager provides a huge software repository for Debian-based distributions like Ubuntu and Kali Linux.

§ 05
Core Usage

The Aptitude Package Manager is used to easily search, install, and manage software packages. sudo (Super User Do) is required to run administrative commands.

apt_examples.sh
sudo apt update # refresh the package index sudo apt install xkcdpass # passphrase generator sudo apt install volatility # memory forensics tool sudo apt remove cowsay # uninstall a package sudo apt search ophcrack # search the repository

Example packages:

xkcdpass — Passphrase generator

ophcrack — Windows password cracker

volatility — Memory forensics

sudo stands for Super User Do — it grants temporary root privileges for a single command.

Permissions

Linux file permissions control who can read, write, or execute a file. Use chmod to modify them.

§ 06
Symbolic Mode
chmod +x script.sh
Change Mode. Adds the executable permission (+x) to a file.
chmod -w file.txt
Removes write permission from a file.
chmod u+x,g-w file
Add execute for user; remove write for group.
Octal (Numeric) Mode

Each group (User / Group / Other) is a sum: read=4, write=2, execute=1.

OctalUserGroupOther
755rwx (7)r-x (5)r-x (5)
644rw- (6)r-- (4)r-- (4)
700rwx (7)--- (0)--- (0)
permissions.sh
chmod +x script.sh # make script executable (symbolic) chmod 755 file # User rwx, Group r-x, Other r-x chmod 644 config.txt # User rw-, Group r--, Other r--

Check Last Command Completion

Every program returns a numeric exit status stored in $? — the variable for the most recent exit code.

§ 07
The $? Variable

The exit status of the last executed program or script is stored in the special variable $?. Check it immediately after a command runs — the next command will overwrite it.

check_status.sh
# Run a command, then check its exit code ls /etc/passwd echo $? # prints: 0 (success) ls /nonexistent echo $? # prints: 2 (error — no such file)

This is a fundamental concept for scripting — check if a command worked before executing the next step.

0 0 = Success
≠0 Non-zero = Error

Changing the Exit Code

Use the exit command to explicitly set your script's exit status and communicate specific results to callers.

§ 08
Custom Exit Status

You can explicitly set the exit code of your script using the exit command, allowing you to communicate specific results or errors back to the terminal or another calling script.

custom_exit.sh
#!/bin/bash echo "Hi there!" exit 22 # After running: # $ ./custom_exit.sh # Hi there! # $ echo $? # 22

Exit codes must be a number from 0–255.

Custom codes help you debug and handle specific failure conditions in complex automation workflows.

0 1 22 127

Try changing the number and re-running the script!

Receive Input from the Command Line

Scripts can receive arguments directly from the command line, accessed via positional variables $1, $2, etc.

§ 09
Positional Arguments

Inputs are accessed via special numeric variables: $1 is the first argument, $2 is the second, and so on. $0 is the script name itself.

greet.sh
#!/bin/bash echo "Hi there!" echo "It's good to see you $1!" exit 0
terminal
# Make the script executable first: chmod +x greet.sh # Run it with an argument: $ ./greet.sh John # Output: # Hi there! # It's good to see you John!

Remember to set execute permissions first: chmod +x my_script.sh

Command-line arguments are essential for making scripts dynamic and reusable.

$0 — Script name
$1 — First argument
$2 — Second argument
$@ — All arguments
$# — Argument count
Best Practice: Always end your scripts with exit 0 on success. It signals to any calling process or CI/CD pipeline that the script completed without errors.