RED HAT ENTERPRISE LINUX
The Shell and
Command Syntax
Access a Shell Prompt and Issue Commands with Correct Syntax
CIS126RH | RHEL System Administration 1
Mesa Community College
The shell is where Linux administration happens. Every task — managing files, configuring services, reading logs, and automating work — begins with a command at a prompt. Understanding how the shell works and how to construct commands correctly is the foundation for this course and the RHCSA exam.
Learning Objectives
- Identify the shell and access a prompt — Distinguish the shell from the kernel; open terminal and virtual console sessions
- Read and interpret the shell prompt — Identify user, host, working directory, and privilege level from the prompt string
- Construct commands with correct syntax — Assemble commands, options, and arguments in the right order
- Use the command line efficiently — Apply history, tab completion, and editing shortcuts to work faster
What is the Shell?
The shell is a program that reads commands you type and runs them. It is the primary interface between a user and the Linux operating system.
- Interprets and executes commands
- Provides scripting, variables, and redirection
- The default shell on RHEL is bash — the Bourne Again SHell
- Other shells exist (
zsh,fish,dash) but bash is the RHCSA standard
Linux System Layers
| Layer | Role |
|---|---|
| Hardware | CPU, RAM, disk, network interfaces |
| Kernel | Manages hardware; provides system calls |
| Shell | Interprets commands; launches programs |
| Utilities | ls, cp, grep, systemctl… |
| You | Type commands; read output; make decisions |
The kernel manages hardware. The shell is a user-space program that talks to the kernel on your behalf. You never interact with the kernel directly.
Accessing a Shell Prompt
Graphical Environment (GNOME)
- GNOME Terminal — Activities → search "terminal"
- Right-click desktop → Open Terminal (if enabled)
Virtual Consoles (Text Mode)
- RHEL provides six virtual consoles: Ctrl+Alt+F1 through F6
- Ctrl+Alt+F1 — GNOME graphical session (if running)
- Ctrl+Alt+F2 through F6 — text login prompts
- Text consoles work even when the graphical interface is broken
Remote Access with SSH
# Connect to a remote RHEL server
$ ssh student@servera
# Log out of a shell session
$ exit # or press Ctrl+D
The exam environment uses SSH to connect to managed nodes. Be comfortable with
ssh, exit, and switching between sessions.
Reading the Shell Prompt
The prompt tells you four things before you type a single character.
Example prompt: [student@servera ~]$
| Prompt element | Meaning |
|---|---|
student | Currently logged-in username |
servera | Hostname of this system |
~ | Current working directory — tilde means home directory |
$ | Regular user (non-root) |
# | Root user — elevated privilege |
# Regular user prompt
[student@servera ~]$
# Root prompt — note the # sign
[root@servera ~]#
# After cd — the directory updates in the prompt
[student@servera ~]$ cd /etc/ssh
[student@servera ssh]$
When the prompt ends with # you are root. Commands run as root take effect
immediately with no safety checks. Work as a regular user whenever possible.
Command Syntax
Every Linux command follows the same structure:
| Part | What it is | Example |
|---|---|---|
| Command | What to do | ls |
| Options | How to do it — modify behaviour | -l |
| Arguments | What to act on | /etc |
# Command only
$ whoami
# Command + option
$ ls -l
# Command + option + argument
$ ls -l /etc
# Multiple options combined after one dash
$ ls -la /etc
# Long-form options use double dash
$ ls --all --human-readable /home
Short options can be combined after a single dash: ls -la equals ls -l -a.
Long options cannot be combined.
Commands in Practice
These orientation commands should become second nature. Run them any time you need to confirm who you are and where you are on the system.
| Command | What it shows |
|---|---|
whoami | Your current username |
id | Your UID, GID, and all group memberships |
pwd | Your current working directory (full path) |
hostname | The name of the system you are logged into |
date | Current date and time |
uptime | How long the system has been running |
$ whoami
student
$ id
uid=1000(student) gid=1000(student) groups=1000(student)
$ cat /etc/redhat-release
Red Hat Enterprise Linux release 9.4 (Plow)
Types of Commands
Not every "command" is the same kind of thing. Use type to find out what you are running.
| Type | Description | Examples |
|---|---|---|
| External | A binary file stored on disk | ls, grep, cp |
| Built-in | Part of bash itself — no file on disk | cd, echo, pwd |
| Alias | A shortcut that expands to another command | ll, rm |
| Function | bash code defined in a config file | custom functions |
$ type ls
ls is aliased to 'ls --color=auto'
$ type cd
cd is a shell builtin
$ type grep
grep is /usr/bin/grep
$ which grep
/usr/bin/grep
Built-ins like cd have no man page — use help cd instead of man cd.
Aliases can shadow external commands unexpectedly.
Options That Take Arguments
Some options require a value immediately after them. The man page SYNOPSIS shows this with a placeholder name.
# man head SYNOPSIS:
head [OPTION]... [FILE]...
# [ ] = optional ... = repeatable
# -n requires a line count
$ head -n 5 /etc/passwd
# Long option: use = or a space — both work
$ head --lines=5 /etc/passwd
# ssh -p requires a port number
$ ssh -p 2222 admin@servera
Forgetting the value after an option that requires one.
head -n /etc/passwd treats the path as the line count —
causing an error or unexpected output. When in doubt run
command --help to see which options need a value.
Command History
bash saves every command you run. History lets you re-use commands without retyping them.
| Key or command | Action |
|---|---|
| ↑ / ↓ | Step backward or forward through history |
| Ctrl+R | Reverse incremental search — start typing to match |
| Ctrl+G | Cancel a history search |
history | List all saved commands with numbers |
!! | Repeat the last command exactly |
!n | Run command number n from the history list |
!string | Run the most recent command that starts with string |
$ history
497 ls -l /etc/ssh
498 grep -v '^#' /etc/ssh/sshd_config
499 systemctl status sshd
$ !498 # re-run command 498
$ sudo !! # repeat last command with sudo
Ctrl+R then start typing is the fastest way to re-run a long command you used earlier in the exam session.
Tab Completion
Press Tab to let bash complete command names, file paths, and subcommands automatically.
| You type | Action | Result |
|---|---|---|
sys then Tab | One unique match | Completes to systemctl |
sys then Tab Tab | Multiple matches | Lists systemctl, sysctl… |
/etc/ssh/sshd then Tab | One unique match | Completes to sshd_config |
systemctl st then Tab | Subcommand completion | Completes to start |
$ cat /etc/ssh/sshd[Tab]
sshd_config
$ systemctl re[Tab][Tab]
reload restart reenable
Tab-complete every file path and command. If completion fails, the path or command does not exist — catching errors before they happen. Press Tab twice to see all possible matches.
Command Line Editing
bash uses Emacs-style key bindings to edit the current command line without retyping it.
| Shortcut | Action |
|---|---|
| Ctrl+A | Move cursor to the beginning of the line |
| Ctrl+E | Move cursor to the end of the line |
| Ctrl+U | Delete everything from cursor to the beginning |
| Ctrl+K | Delete everything from cursor to the end |
| Ctrl+W | Delete the word immediately to the left |
| Ctrl+C | Cancel — interrupt the current command |
| Ctrl+L | Clear the screen (same as clear) |
| Ctrl+D | Log out — signals end of input |
Ctrl+A (beginning), Ctrl+E (end), and Ctrl+U (clear left) are the three most valuable shortcuts for correcting long commands quickly on a timed exam.
Shell Variables and the Environment
bash stores configuration in variables. Variables exported to child processes form the environment.
| Variable | Contains |
|---|---|
$HOME | Your home directory path |
$USER | Your login name |
$PATH | Directories searched when you run a command |
$PWD | Current working directory |
$SHELL | Path to your login shell |
$HISTSIZE | Number of commands kept in history |
$ echo $HOME
/home/student
$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
$ MYVAR=hello ; export MYVAR
$PATH determines which directories are searched when you type a command name.
If a command is "not found," its directory is likely missing from $PATH.
Quoting and Special Characters
The shell interprets certain characters specially. Quoting controls when that interpretation happens.
| Method | Effect |
|---|---|
'single quotes' | Preserve everything literally — no substitution at all |
"double quotes" | Allow variable and command substitution inside |
Backslash \ | Escape the very next character only |
# Variable NOT expanded — single quotes
$ echo '$HOME'
$HOME
# Variable IS expanded — double quotes
$ echo "$HOME"
/home/student
# Command substitution inside double quotes
$ echo "Today is $(date +%A)"
Today is Monday
Use single quotes when you want the shell to treat the string exactly as written. Use double quotes when you need variable expansion inside the string.
Running Multiple Commands
bash provides operators for combining and sequencing commands on one line.
| Operator | Meaning |
|---|---|
; | Run the second command regardless of the first result |
&& | Run the second command only if the first succeeded |
|| | Run the second command only if the first failed |
& | Run the command in the background |
| Backslash at end of line | Continue the command on the next line |
# && — second runs only on success
$ mkdir /tmp/mydir && cd /tmp/mydir
# || — second runs only on failure
$ ping -c1 server1 || echo "unreachable"
# Line continuation for readability
$ grep -vE '^(#|$)' \
/etc/ssh/sshd_config
Prefer && over ; when the second command depends on the
first. It prevents a cascade of errors if an early step fails.
Becoming Root Safely
| Method | Requires | Best for |
|---|---|---|
sudo command | User listed in sudoers | A single privileged command |
sudo -i | User listed in sudoers | Multiple root tasks; keeps an audit trail |
su - | Root password | Legacy systems without sudo configured |
# Run one command as root
$ sudo systemctl restart sshd
# Open a full root login shell
$ sudo -i
[root@servera ~]#
[root@servera ~]# exit
$
On RHEL 9, sudo is the recommended method. It logs every privileged command
to /var/log/secure, providing an audit trail that su does not.
On the exam, the student account has full sudo rights.
Use sudo -i to open a root shell when you have several privileged tasks to complete.
Knowledge Check
Answer these before moving to the next slide.
- What does a
#at the end of the shell prompt tell you? - What is the correct order of parts in a Linux command?
- You type a command and get "command not found." Name two things you would check.
- What is the difference between
&∧when combining commands? - What keyboard shortcut moves the cursor to the beginning of the current command line?
- What is the difference between
sudo commandandsudo -i?
Knowledge Check — Answers
- A
#prompt means you are running as root — full system privilege with no safety checks. - Command, then options, then arguments — in that order. Example:
ls -lh /etc - Check that the command name is spelled correctly (use tab completion to verify), and run
type commandnameorwhich commandname. If nothing is found, the package may not be installed or the directory is not in$PATH. &&runs the second command only if the first succeeded (exit code 0).;always runs the second command regardless of success or failure.- Ctrl+A moves the cursor to the beginning. Ctrl+E moves to the end.
sudo commandruns a single command as root then returns to your normal shell.sudo -iopens a full root login shell — useful when you have many privileged tasks to complete.
Key Takeaways
-
Read the prompt before every command.
It tells you who you are (
$= user,#= root), where you are (working directory), and which host you are on. - Every command follows the same structure: command, then options, then arguments. Options come before arguments. Short options can be combined; long options cannot.
- Tab completion and command history eliminate most retyping. Use ↑ or Ctrl+R to recall commands. Use Ctrl+A, Ctrl+E, and Ctrl+U to fix long commands quickly.
-
Use
sudofor elevated privilege.sudo commandfor one task;sudo -ifor a root shell session. Always check your prompt — one wrong keystroke as root can cause serious damage.
Graded Lab
- Open a terminal and use
whoami,id,hostname, andpwdto confirm your identity and location - Use
typeto determine whetherls,cd, andgrepare external commands, built-ins, or aliases - Run a command with incorrect syntax, read the error message, and correct it
- Use Ctrl+R to find and re-run a command from your history without retyping it
- Use
sudo -ito open a root shell, confirm the prompt changes to#, thenexitback to your user shell - Combine two commands with
&&so the second runs only if the first succeeds
"Access a shell prompt and issue commands with correct syntax." — Every task on the exam starts here.