root@thehacksparrow:~/writeups$ SYSTEM ONLINE
root@sparrow:~/writeups$ cat linux-shells.md
// Fundamentals

Linux Shells

23 Nov 2026 · 6 min read
Overview — A beginner Fundamentals room about the Linux command-line shell: what a shell is, how to reach one over SSH, the everyday commands you use in it, how Bash, Fish and Zsh differ, and finally how to automate work with shell scripts built from variables, loops, conditionals and comments. It ends with two hands-on scripts: a "locker" PIN checker you write yourself, and a log-searching script you repair and run on the lab VM.
PlatformTryHackMe
CategoryFundamentals
DifficultyEasy
RoomLinux Shells

Task 1 — Introduction to Linux Shells

A graphical interface is convenient, but almost everything an operating system can do can also be done by typing commands, and doing it that way is faster, scriptable and lighter on resources. The program that accepts those typed commands, interprets them and asks the kernel to carry them out is the shell. It sits between you and the operating system. The room's goals: interact with a shell, use basic commands, compare the shells that exist, and write simple scripts.

Q: Who is the facilitator between the user and the OS?
A: Shell

Task 2 — How to Interact with a Shell

The lab machine is reached with SSH (or from the in-browser AttackBox). SSH gives you an encrypted remote shell; on first contact it shows the server's host-key fingerprint and asks you to trust it, then stores it so later connections can detect a changed key.

ssh user@MACHINE_IP

After logging in you land in your home directory in the distribution's default shell, which on most distributions is Bash (Bourne Again Shell). The most important commands to start with:

  • pwd — print the working directory, so you always know where you are.
  • cd — change directory.
  • ls — list directory contents.
  • grep — search for a word or regular-expression pattern inside files or piped output. Essential for pulling a few relevant lines out of a huge file or log.

Q: What is the default shell in most Linux distributions?
A: Bash

Q: Which command utility is used to list down the contents of a directory?
A: ls

Q: Which command utility can help you search for anything in a file?
A: grep

Task 3 — Types of Linux Shells

Linux, like Windows with Command Prompt and PowerShell, has several shells. Two quick ways to find out which you have:

echo $SHELL        # your login shell, e.g. /bin/bash
cat /etc/shells    # every shell installed and allowed as a login shell

Note that $SHELL shows your login shell, not necessarily the one you are running if you started another from inside it. /etc/shells on the lab lists sh, bash, rbash (restricted bash), dash, zsh, tmux and screen. To try one, just type its name (for example zsh); to make it your default permanently use chsh -s /usr/bin/zsh (the path must be listed in /etc/shells).

Bash

The default on most systems and a superset of ideas from older shells (sh, ksh, csh). It has strong scripting support and the widest documentation, tab completion, and a command history: arrow keys recall earlier commands and history prints them.

Fish (Friendly Interactive Shell)

Built for usability: simple syntax, built-in syntax highlighting (errors show up in colour as you type), autosuggestions from previous commands, auto spell correction and theme-able prompts. Its scripting language is deliberately not POSIX-compatible, so scripts written for Bash will not run unchanged.

Zsh (Z Shell)

A modern shell combining Bash-style scripting with extras: powerful, extensible completion, spelling correction and deep customisation (commonly via frameworks such as oh-my-zsh), at the cost of some speed when heavily customised.

FeatureBashFishZsh
ScriptingWidely compatible, best documentedMore limited, different syntaxExcellent, mostly Bash-compatible
Tab completionBasicAdvanced, history-based suggestionsVery extensible with plugins
CustomisationBasicGood, interactive toolsAdvanced
Syntax highlightingNot built inBuilt inVia plugins
FriendlinessFamiliar but plainMost beginner-friendlyHigh once configured

Q: Which shell comes with syntax highlighting as an out-of-the-box feature?
A: Fish

Q: Which shell does not have auto spell correction?
A: Bash

Q: Which command displays all the previously executed commands of the current session?
A: history

Task 4 — Shell Scripting and Components

A shell script is a text file of commands run in sequence. Instead of retyping a multi-step routine, you write it once and execute it. Shell scripts are not the only way to script (Python and others work too), but this room stays with the shell. By convention Bash scripts end in .sh, though the extension is only a label.

Every script should begin with a shebang, #! followed by the interpreter's path, so the kernel knows which program should run the file:

#!/bin/bash
The shebang only works as the very first line of the file. The room's snippets put a comment above it, which would turn it into an ordinary comment; keep the shebang first and put comments after it.

Variables

A variable stores a value you want to reuse, such as a path, URL or user input. read captures a line typed by the user into a variable, and $name expands it:

#!/bin/bash
echo "Hey, what's your name?"
read name
echo "Welcome, $name"

Make the file executable and run it. The ./ prefix is needed because the current directory is normally not in PATH; without it the shell searches only PATH directories and reports "command not found".

chmod +x variable_script.sh
./variable_script.sh

Loops

A loop repeats a block of code. This for loop uses brace expansion to count from 1 to 10; do opens the body and done closes it:

#!/bin/bash
for i in {1..10}; do
    echo $i
done

Conditional statements

Conditionals run code only when a test is true. The spaces inside [ ] are mandatory because [ is itself a command, and quoting the variable avoids errors when it is empty or contains spaces. fi (if backwards) closes the block:

#!/bin/bash
echo "Please enter your name first:"
read name
if [ "$name" = "Stewart" ]; then
    echo "Welcome Stewart! Here is the secret."
else
    echo "Sorry! You are not authorized to access the secret."
fi

Comments

Anything after a # (apart from the shebang line) is ignored by the shell. Comment the non-obvious parts of a script, for your future self and for anyone you share it with; commenting every single line is more noise than help.

Q: What is the shebang used in a Bash script?
A: #!/bin/bash

Q: Which command gives executable permissions to a script?
A: chmod +x

Q: Which scripting functionality helps us configure iterative tasks?
A: loops

Task 5 — The Locker Script

The exercise combines all three constructs. The spec: ask for a user name, company name and PIN, and grant access only if all three match (John, Tryhackme, 7385). The room's approach loops three times and uses if/elif/else inside the loop to decide which question to ask on each pass, then does a final combined check with && (logical AND):

#!/bin/bash
username=""
companyname=""
pin=""

for i in {1..3}; do
    if [ "$i" -eq 1 ]; then
        echo "Enter your Username:"
        read username
    elif [ "$i" -eq 2 ]; then
        echo "Enter your Company name:"
        read companyname
    else
        echo "Enter your PIN:"
        read pin
    fi
done

if [ "$username" = "John" ] && [ "$companyname" = "Tryhackme" ] && [ "$pin" = "7385" ]; then
    echo "Authentication Successful. You can now access your locker, John."
else
    echo "Authentication Denied!!"
fi

-eq compares integers while = compares strings, which is why the loop counter and the text fields use different operators. Running it with a wrong PIN prints the denial message. (The loop is a teaching device; three plain read lines would be simpler. Also, hard-coding secrets in a script, as here, is only acceptable in an exercise.)

Q: What would be the correct PIN to authenticate in the locker script?
A: 7385

Task 6 — Practical Exercise

A half-finished script flag_hunt.sh sits in the user's home directory. It should scan every .log file in a directory and report which one contains a given keyword. Three empty string assignments ("") must be filled in, then it can be run. The room recommends working as root so every log is readable:

sudo su

The keyword to search is the room's flag-style string (shown here as FLAG) and the directory is /var/log. The finished script:

#!/bin/bash

directory="/var/log"
flag="FLAG"      # replace with the keyword given in the task

echo "Flag search in directory: $directory in progress..."

for file in $directory/*.log; do
    if grep -q "$flag" "$file"; then
        echo "Flag found in: $(basename "$file")"
    fi
done
  • $directory/*.log is a glob the shell expands to every matching file, so the loop variable takes each path in turn.
  • grep -q is quiet mode: it prints nothing and just sets the exit status, which is exactly what if tests.
  • $(basename "$file") is command substitution that strips the directory, leaving only the file name. Quoting "$file" protects names containing spaces.
Flag search in directory: /var/log in progress...
Flag found in: authentication.log

Q: Which file has the keyword?
A: authentication.log

Reading that file with cat /var/log/authentication.log shows a short sentence next to the keyword saying where the cat is sleeping.

Q: Where is the cat sleeping?
A: under the table

Key takeaways

  • The shell is the interface to the OS; Bash is the default and the one to learn first, while Fish and Zsh trade compatibility or speed for comfort.
  • Know which shell you are in (echo $SHELL) and which are available (/etc/shells); this matters later when stabilising reverse shells or escaping restricted shells.
  • A script is a file starting with a shebang, made executable with chmod +x and run with ./.
  • Variables, loops and conditionals cover most basic automation, and grep combined with a loop is the seed of many log-hunting and recon scripts.
  • Quote your variables, mind the spaces in [ ], and never keep real secrets hard-coded in scripts.