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

  1. Identify the shell and access a prompt — Distinguish the shell from the kernel; open terminal and virtual console sessions
  2. Read and interpret the shell prompt — Identify user, host, working directory, and privilege level from the prompt string
  3. Construct commands with correct syntax — Assemble commands, options, and arguments in the right order
  4. 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

LayerRole
HardwareCPU, RAM, disk, network interfaces
KernelManages hardware; provides system calls
ShellInterprets commands; launches programs
Utilitiesls, cp, grep, systemctl
YouType commands; read output; make decisions
Key concept

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
RHCSA Note

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 elementMeaning
studentCurrently logged-in username
serveraHostname 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]$
Root Warning

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
CommandWhat to dols
OptionsHow to do it — modify behaviour-l
ArgumentsWhat 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
Tip

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.

CommandWhat it shows
whoamiYour current username
idYour UID, GID, and all group memberships
pwdYour current working directory (full path)
hostnameThe name of the system you are logged into
dateCurrent date and time
uptimeHow 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.

TypeDescriptionExamples
ExternalA binary file stored on diskls, grep, cp
Built-inPart of bash itself — no file on diskcd, echo, pwd
AliasA shortcut that expands to another commandll, rm
Functionbash code defined in a config filecustom 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
Why it matters

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
Common Mistake

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 commandAction
/ Step backward or forward through history
Ctrl+RReverse incremental search — start typing to match
Ctrl+GCancel a history search
historyList all saved commands with numbers
!!Repeat the last command exactly
!nRun command number n from the history list
!stringRun 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
Exam Tip

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 typeActionResult
sys then TabOne unique matchCompletes to systemctl
sys then Tab TabMultiple matchesLists systemctl, sysctl
/etc/ssh/sshd then TabOne unique matchCompletes to sshd_config
systemctl st then TabSubcommand completionCompletes to start
$ cat /etc/ssh/sshd[Tab]
sshd_config

$ systemctl re[Tab][Tab]
reload    restart   reenable
Build this habit

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.

ShortcutAction
Ctrl+AMove cursor to the beginning of the line
Ctrl+EMove cursor to the end of the line
Ctrl+UDelete everything from cursor to the beginning
Ctrl+KDelete everything from cursor to the end
Ctrl+WDelete the word immediately to the left
Ctrl+CCancel — interrupt the current command
Ctrl+LClear the screen (same as clear)
Ctrl+DLog out — signals end of input
Exam Efficiency

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.

VariableContains
$HOMEYour home directory path
$USERYour login name
$PATHDirectories searched when you run a command
$PWDCurrent working directory
$SHELLPath to your login shell
$HISTSIZENumber 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

$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.

MethodEffect
'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
Single vs Double Quotes

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.

OperatorMeaning
;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 lineContinue 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
Tip

Prefer && over ; when the second command depends on the first. It prevents a cascade of errors if an early step fails.

Becoming Root Safely

MethodRequiresBest for
sudo commandUser listed in sudoersA single privileged command
sudo -iUser listed in sudoersMultiple root tasks; keeps an audit trail
su -Root passwordLegacy 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
$
Use sudo, not su

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.

RHCSA Note

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.

  1. What does a # at the end of the shell prompt tell you?
  2. What is the correct order of parts in a Linux command?
  3. You type a command and get "command not found." Name two things you would check.
  4. What is the difference between && and ; when combining commands?
  5. What keyboard shortcut moves the cursor to the beginning of the current command line?
  6. What is the difference between sudo command and sudo -i?

Knowledge Check — Answers

  1. A # prompt means you are running as root — full system privilege with no safety checks.
  2. Command, then options, then arguments — in that order. Example: ls -lh /etc
  3. Check that the command name is spelled correctly (use tab completion to verify), and run type commandname or which commandname. If nothing is found, the package may not be installed or the directory is not in $PATH.
  4. && runs the second command only if the first succeeded (exit code 0). ; always runs the second command regardless of success or failure.
  5. Ctrl+A moves the cursor to the beginning. Ctrl+E moves to the end.
  6. sudo command runs a single command as root then returns to your normal shell. sudo -i opens a full root login shell — useful when you have many privileged tasks to complete.

Key Takeaways

  1. 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.
  2. Every command follows the same structure: command, then options, then arguments. Options come before arguments. Short options can be combined; long options cannot.
  3. 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.
  4. Use sudo for elevated privilege. sudo command for one task; sudo -i for 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, and pwd to confirm your identity and location
  • Use type to determine whether ls, cd, and grep are 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 -i to open a root shell, confirm the prompt changes to #, then exit back to your user shell
  • Combine two commands with && so the second runs only if the first succeeds
RHCSA Objective

"Access a shell prompt and issue commands with correct syntax." — Every task on the exam starts here.