Red Hat System Administration I · RH124

Chapter 2

Accessing the Command Line
The bash shell · Command syntax · Getting help
CIS126RH — Mesa Community College

Chapter Objective

Log in to a Red Hat Enterprise Linux system's graphical and command-line environments, and run simple commands using the bash shell correctly.

Key Commands

  • bash
  • pwd
  • echo
  • history
  • man
  • apropos
  • which

What Is the Shell?

The shell is a command interpreter that reads what you type, runs the corresponding program, and displays the results. On Red Hat Enterprise Linux, the default shell is bash (the Bourne Again SHell).

Every time you open a terminal, you're running an instance of the shell. It reads commands one line at a time, executes them, and prints a new prompt when it's ready for the next command.

Why It Matters — Nearly everything you do as a system administrator — installing software, managing users, configuring services, automating tasks — happens through the shell. The GUI is optional; the command line is not.

The Shell Prompt

The default prompt shows your username, hostname, and current directory, ending in $ for a regular user or # for the root user.

# Regular user prompt
[student@server1 ~]$

# Root user prompt (note the #)
[root@server1 ~]#
Warning — Know Your Prompt — Always check whether your prompt ends in $ or # before running a command. A # prompt means you are root and can damage the system with a single mistyped command.

Accessing a Terminal

RHEL provides several ways to reach a shell prompt, depending on whether the system has a graphical desktop installed.

MethodWhereTypical Use
GNOME TerminalGraphical desktopLocal workstation use
Virtual consoleCtrl+Alt+F3F6Direct console login, no GUI needed
SSHRemote network loginManaging remote servers
Text-mode boot targetServers with no GUIMost production RHEL servers

Switching Virtual Consoles

RHEL provides multiple virtual consoles. From the graphical desktop, switch to a text console with Ctrl+Alt+F3 (through F6), and return to the graphical session with Ctrl+Alt+F1 or F2.

Tip — Most Servers Are Headless — Production RHEL servers usually boot to a text-mode target with no desktop installed at all. Comfort at the command line isn't optional — it's the primary interface.

Command Syntax

Most commands follow the same general pattern:

command [options] [arguments]
# A command with no options or arguments
pwd

# A short option
ls -l

# A long option
ls --all

# Short options can often be combined
ls -la

# A command with an option and an argument
ls -l /etc

Quoting and Special Characters

The shell treats spaces, quotes, and characters like *, $, and > specially. Quote arguments that contain spaces so the shell treats them as a single value.

# Without quotes, the shell sees two arguments
echo Hello World

# With quotes, the shell sees one argument
echo "Hello World"

# Single quotes prevent the shell from expanding variables
echo '$HOME stays literal here'

# Double quotes still allow variable expansion
echo "$HOME expands here"
Exam Note — Know the difference between single quotes (fully literal) and double quotes (still expand $variables and command substitution). This trips up new administrators constantly.

Command-Line Editing and History

Bash keeps a history of commands you've run and provides keyboard shortcuts, inherited from the Emacs editing model, for editing the current line without retyping it.

ShortcutAction
Ctrl+AMove cursor to the start of the line
Ctrl+EMove cursor to the end of the line
Ctrl+UDelete from cursor to start of line
Ctrl+KDelete from cursor to end of line
Ctrl+RSearch backward through command history
Ctrl+CCancel the current command
/ Step backward / forward through history

Working with History

# Show your recent command history
history

# Re-run command number 482 from history
!482

# Re-run the most recent command
!!

# Search history interactively (press Ctrl+R, then type)
(reverse-i-search)`ssh': ssh student@server1
Tip — Ctrl+R Is Your Friend — Instead of pressing ↑ dozens of times, press Ctrl+R and start typing a fragment of the command you want. Press Ctrl+R again to cycle to earlier matches.

Files, Paths, and Tab Completion

Every command that expects a file or directory understands both absolute paths (starting from /) and relative paths (starting from your current directory).

# Show your current working directory
pwd

# Absolute path — always starts from root
cd /etc/ssh

# Relative path — starts from wherever you currently are
cd ../ssh

# Shortcuts
cd ~        # your home directory
cd -        # the previous directory
cd ..       # the parent directory

Tab Completion

Press Tab while typing a command, file name, or path and bash will complete it automatically. Press Tab twice to list all possible matches when more than one exists.

# Type this, then press Tab
cd /etc/ss[Tab]
# bash completes to:
cd /etc/ssh/
Tip — Type Less, Tab More — Tab completion isn't just a convenience — it prevents typos in long paths and confirms a file or directory actually exists before you hit Enter.

Getting Help at the Command Line

RHEL ships extensive built-in documentation. You rarely need to leave the terminal to learn how a command works.

ToolPurpose
manFull manual page for a command, file, or system call
--helpQuick summary of a command's options
aproposSearch man page descriptions by keyword
whichShow the full path to a command's executable
# Read the full manual page for a command
man ssh

# Quick reference of a command's options
ssh --help

# Search man page descriptions for a keyword
apropos partition

# Find the exact program a command runs
which ssh

Navigating a man Page

KeyAction
SpaceScroll forward one page
bScroll backward one page
/patternSearch forward for pattern
nRepeat the last search
qQuit the man page
Warning — Man Pages Are Organized in Sections — Some names exist in more than one man section (for example, passwd the command versus passwd the file format). Use man 5 passwd to request a specific section when the default result isn't the one you need.

Key Terms for Chapter 2

shell
A command interpreter that reads, executes, and returns output for commands
bash
The Bourne Again SHell; the default shell on RHEL
prompt
The text shown when the shell is ready for input; ends in $ (user) or # (root)
virtual console
A separate text-mode login session, reached with Ctrl+Alt+F3F6
option
A flag that modifies a command's behavior, usually prefixed with - or --
argument
The file, directory, or value a command acts on
absolute path
A file path that starts from the root directory /
relative path
A file path interpreted from the current working directory
tab completion
Bash feature that auto-completes commands, files, and paths when Tab is pressed
man page
The built-in manual page documenting a command, file format, or system call
apropos
Searches man page descriptions for a keyword
history
The list of previously run commands in the current shell session

Review Questions

  1. What is the difference between a $ prompt and a # prompt?
  2. Name two ways to reach a shell prompt on a RHEL system that has no graphical desktop installed.
  3. What is the difference between how single quotes and double quotes handle a variable like $HOME?
  4. You want to re-run the command you ran three commands ago without retyping it. Name two different ways to do this.
  5. What does pressing Tab twice do when there is more than one possible completion?
  6. What is the difference between an absolute path and a relative path? Give an example of each.
  7. You run man passwd but get the wrong page for what you need. What command would get you the file-format version instead?
1 / 16