Linux: Terminal Basics — Shell and Your First Command

The terminal is the primary way developers interact with Linux. It lacks the flashiness of a graphical interface, but its efficiency — once you get the hang of it — far surpasses clicking a mouse.

📋 Prerequisites: You should first complete

1. What You Will Learn


2. A Beginner's First Terminal Experience

(1) The Pain: A Black Window with White Text Is Intimidating

Alex opened the WSL2 terminal and saw only a black window with a blinking cursor. He didn't know what to type or what would happen when he pressed Enter. He didn't even know how to exit.

(2) The First Command

Senior engineer Bob said: "Say hello first. Type echo "Hello Linux" and press Enter."

BASH
echo "Hello Linux"
# Output: Hello Linux

The terminal echoed back what he typed — Alex realized the terminal is like a conversation window: you type a command, and the computer replies with the result.

(3) The Benefit: From Anxiety to Control

After learning his first command, Alex tried date, whoami, and cal. Within 5 minutes, he went from "afraid to touch the terminal" to "able to read terminal output."


3. Knowledge Points

(1) Shell and Terminal

A terminal emulator is the window you see — GNOME Terminal on Linux, Windows Terminal on Windows, Terminal.app on macOS.

The Shell is the program running inside the terminal, responsible for interpreting the commands you type. The most common Shell is bash (Bourne Again Shell).

TEXT
Terminal window → Shell (bash) → Operating System

(2) General Command Syntax

Almost all Linux commands follow this structure:

TEXT

COMMAND [OPTIONS] [ARGUMENTS]
Part Explanation Example
COMMAND The name of the command to execute ls
OPTIONS Options that modify command behavior (usually start with -) -l (long format), -a (show hidden)
ARGUMENTS The target the command operates on Filename, directory name
TEXT
# Syntax example
ls -la /home
# Command: ls
# Options: -l (long format) and -a (hidden files)
# Argument: /home (directory to list)

(3) Tab Auto-Completion

The Tab key is the most useful key in the terminal. You only need to type part of a command or filename, and pressing Tab will auto-complete it.

💡 Tip: Tab completion doesn't just save typing time — more importantly, it prevents spelling errors. If pressing Tab doesn't complete anything, it means the path or command name you typed may be wrong. Combined with Ctrl+R reverse history search, your efficiency can increase several times.

Tab effect Description
Auto-complete Automatically completes the full name when there's a unique match
Show all matches When there are multiple matches, one press does nothing; two presses show all options
Complete command Type ec + Tab → auto-completes to echo
Complete file cd /ho + Tab → auto-completes to cd /home
Complete path ls ~/Doc + Tab → ls ~/Documents/

(4) Terminal Keyboard Shortcuts

Shortcut Action
Ctrl + C Terminate the currently running program (not copy!)
Ctrl + D Exit the current Shell or end input (EOF)
Ctrl + L Clear the screen (same as clear command)
Ctrl + R Reverse search through command history
/ Browse command history
Ctrl + A Move cursor to the beginning of the line
Ctrl + E Move cursor to the end of the line
Ctrl + U Delete all characters before the cursor
Ctrl + K Delete all characters after the cursor
Ctrl + W Delete the word before the cursor
💡 Tip: Terminal shortcuts follow the Emacs style — Ctrl+A goes to line start (like Emacs), Ctrl+E goes to line end, Ctrl+U clears before cursor, Ctrl+K clears after cursor, Ctrl+W deletes the previous word. Memorize these combinations and editing long commands becomes much faster.

(5) ▶ Example: Basic Interactive Commands

BASH
# View current date and time
date
# Output: Tue Jul  7 10:23:45 CST 2026

# Print current username
whoami
# Output: alice

# View calendar
cal
#       July 2026
# Su Mo Tu We Th Fr Sa
#           1  2  3  4
#  5  6  7  8  9 10 11
# ...

# View system uptime
uptime
# Output: 10:23  up 2 days, 2 users

Output:

TEXT
Tue Jul  7 10:23:45 CST 2026
alice
     July 2026
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
10:23  up 2 days, 2 users

(6) ▶ Example: Various Uses of the echo Command

TEXT
# Print a string
echo "Hello Linux"

# Print a variable
echo "Current user is: $USER"

# Print command output
echo "Today is $(date +%A)"

# Write to a file
echo "Hello World" > welcome.txt

(7) ▶ Example: Tab Completion Practice

TEXT
# Type cd /h then press Tab → auto-completes /home
cd /home

# Type ls -l /e then press Tab → auto-completes /etc
ls -l /etc

# Press Tab twice to show matches (type cd /e + Tab + Tab)
# Lists all paths starting with /e

(8) ▶ Example: Clearing the Screen and Exiting

TEXT
# Too much on screen? Clear it
clear

# Or use the shortcut Ctrl + L

# Exit the terminal
exit

# Or press Ctrl + D

(9) ▶ Example: Getting Command Help

TEXT
# Method 1: --help option (supported by most commands)
ls --help | head -20

# Method 2: man pages (most detailed)
man ls
# Press q to exit the man page

# Method 3: whatis one-line description
whatis ls
# Output: ls (1) - list directory contents

(10) ▶ Comprehensive Example: Alex's First Terminal Adventure

BASH
# After opening the terminal, Alex tried the following
echo "Hello Linux"          # Say hello to the computer
date                        # What's today's date?
whoami                      # Who am I?
uptime                      # How long has the server been running?
cal                         # Show this month's calendar
ls -la                      # What files are in my home directory?
echo $SHELL                 # What Shell am I using?
exit                        # Goodbye, terminal!

Output:

TEXT
Hello Linux
Tue Jul  8 10:23:45 CST 2026
alice
 10:23:45 up 3 days, 2:10,  1 user,  load average: 0.00, 0.01, 0.05
     July 2026
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
total 28
drwxr-xr-x  1 alice alice 4096 Jul  8 10:23 .
drwxr-xr-x  1 alice alice 4096 Jul  8 10:23 ..
-rw-r--r--  1 alice alice  220 Jul  8 10:23 .bash_logout
-rw-r--r--  1 alice alice 3771 Jul  8 10:23 .bashrc
-rw-r--r--  1 alice alice  807 Jul  8 10:23 .profile
/bin/bash

💡 Alex has learned the most basic terminal commands — he'll continue building on these in the following chapters!


❓ FAQ

Q: What's the difference between a terminal and a Shell? A: A terminal is the window program (e.g., GNOME Terminal, Windows Terminal), while a Shell is the command interpreter running inside the terminal (e.g., bash, zsh). The terminal provides the "window," and the Shell is "the person talking to you."

Q: What can Tab complete in the terminal? A: Command names, filenames, directory names, command options (some programs support this), and environment variable names (when starting with $). On Linux, Ctrl + I and Tab are the same thing.

Q: What if pressing Ctrl + C doesn't stop the program? A: First try Ctrl + C, then Ctrl + Z to suspend the program, followed by kill %1 to terminate it. If that still doesn't work, use ps aux | grep program_name to find the PID and then kill -9 PID.

Q: What's the difference between clear and Ctrl + L? A: Same effect — both clear the screen. But clear is a command (clears the terminal and scroll buffer), while Ctrl + L is a shortcut (only clears the visible portion). You can use either in practice.

Q: How do I view previously entered commands? A: Press to browse one by one. Press Ctrl + R to enter reverse search mode and search history by keyword. All history is saved in ~/.bash_history.


📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Open the terminal, type echo "Hello Linux" and observe the output, then use date and cal to view the current date and calendar
  2. Intermediate (Difficulty ⭐⭐): Read the ls command manual with man ls (press q to exit), and practice Tab completion — type cd / then press Tab twice to see what directories exist
  3. Advanced (Difficulty ⭐⭐⭐): Create a Shell alias today that displays both the date and calendar; then use Ctrl + R to search your command history and list your 5 most-used terminal shortcuts
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏