NANDHOO.

Chapter 2: CLI Fundamentals

Mastering the command line requires more than just memorizing names like ls or cd. You must understand the underlying syntax and logic that the shell uses to parse and execute your requests.

I. The Anatomy of a Command

A command string is typically composed of three logical components: the executable, its behavior-modifying flags, and the target arguments.

tar -czf backup.tar.gz ./srcCOMMANDFLAGS (Options)ARGUMENTS (Targets)

  1. Command: The binary or script to execute (e.g., tar).
  2. Flags (Options): Instructions that modify the command's logic.
    • tar -c: Create an archive.
    • tar -z: Compress using Gzip.
    • tar -f: Target a specific File.
  3. Arguments: The data the command processes (the name of the archive and the source folder).

II. Command Execution Logic

When you press Enter, the shell follows a strict order of operations:

  1. Parsing: The shell breaks the string into "tokens" based on spaces.
  2. Expansion: The shell replaces wildcards (like *) and variables (like $USER) with their actual values.
  3. Lookup: The shell looks for the command name in your $PATH (more on this in Chapter 4).
  4. Execution: The shell forks a new process and runs the program.
  5. Exit Status: The program returns a number (0 for success, non-zero for failure) to the shell.

III. Shell Expansion (Globbing)

One of the most powerful features of the CLI is Globbing—the ability to target multiple files using patterns.

PatternMeaningExample
*Matches any number of charactersrm *.log (Delete all log files)
?Matches exactly one characterls file?.txt (Matches file1.txt, fileA.txt)
[abc]Matches any one character in the setls image[123].png
{a,b}Brace expansion (Generates strings)mkdir {src,bin,docs}

IV. Quoting and Escaping

Because the shell gives special meaning to spaces and characters like *, you must use quotes when you want to treat them as literal text.

  • Single Quotes (' '): Everything inside is literal. No expansion happens.
    • echo '$USER' outputs $USER.
  • Double Quotes (" "): Most characters are literal, but variables and command substitutions are expanded.
    • echo "$USER" outputs jane.
  • Backslash (\): Escapes the next character.
    • echo "It cost \$5" outputs It cost $5.

V. Advanced Keyboard Shortcuts

ShortcutAction
Ctrl + AMove cursor to the Atart (beginning) of the line.
Ctrl + EMove cursor to the End of the line.
Ctrl + WDelete the word behind the cursor.
Ctrl + UDelete the entire line before the cursor.
Ctrl + RReverse search through your command history.
Alt + .Paste the last argument of the previous command.

In the next chapter, we will learn how to navigate the file system with surgical precision using Absolute and Relative paths.