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.
admin@kasykey:~$~ represents the Home Directory.lscat hello.txtcd Desktopcd ..Installing Programs
Use sudo apt to install software. The pipe | lets you chain commands together powerfully.
sudo apt search passwordsudo grants Administrator permissions.sudo apt install cowsayfiglet hello | cowsay -n|): 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.
cat names.txt | sort > sorted.txt
Folder and File Management
Create, copy, move, and delete files and directories from the command line.
mkdir new_folder/sub_foldercp file.txt ~/Desktopfile.txt to the Desktop directory (~ = home folder).mv old_name.txt new_name.txtrmdir empty_folderrm file.txtApt Package Manager Details
The Aptitude package manager provides a huge software repository for Debian-based distributions like Ubuntu and Kali Linux.
The Aptitude Package Manager is used to easily search, install, and manage software packages.
sudo (Super User Do) is required to run administrative commands.
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.
chmod +x script.sh+x) to a file.chmod -w file.txtchmod u+x,g-w fileEach group (User / Group / Other) is a sum: read=4, write=2, execute=1.
| Octal | User | Group | Other |
|---|---|---|---|
| 755 | rwx (7) | r-x (5) | r-x (5) |
| 644 | rw- (6) | r-- (4) | r-- (4) |
| 700 | rwx (7) | --- (0) | --- (0) |
Check Last Command Completion
Every program returns a numeric exit status stored in $? — the variable for the most recent exit code.
$? 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.
This is a fundamental concept for scripting — check if a command worked before executing the next step.
Changing the Exit Code
Use the exit command to explicitly set your script's exit status and communicate specific results to callers.
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.
Exit codes must be a number from 0–255.
Custom codes help you debug and handle specific failure conditions in complex automation workflows.
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.
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.
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 countexit 0 on success. It signals to any calling process or CI/CD pipeline that the script completed without errors.