Ensiklopedia VibeKoding: An Introduction to Command Line and Shell Scripts.Ensiklopedia VibeKoding: An Introduction to Command Line and Shell Scripts.
> π‘ Learning Guide: This chapter aims to provide readers with zero prior experience a systematic understanding of how the terminal (Terminal) works. No computer science background is required β we will use interactive demonstrations to explain the terminal's operating mechanisms step by step.> π‘ Learning Guide: This chapter aims to provide readers with zero prior experience a systematic understanding of how the terminal (Terminal) works. No computer science background is required β we will use interactive demonstrations to explain the terminal's operating mechanisms step by step.
Before you start learning, you first need to find it. The terminal comes pre-installed on every operating system β you don't need to install any additional software to use it.Before you start learning, you first need to find it. The terminal comes pre-installed on every operating system β you don't need to install any additional software to use it.
ο£Ώ macOS (Mac) 1. Press Command (β) + Space to open Spotlight Search. 2. Type Terminal. 3. Press Enter, and you'll see a window with white text on black (or black text on white). πͺ Windows - Method 1 (CMD): Press Win + R, type cmd, and press Enter. This is the oldest command line. - Method 2 (PowerShell): Press Win + R, type powershell, and press Enter. This is a more modern and powerful terminal. - _Tip: Either works for simple tasks, but PowerShell or WSL (Windows Subsystem for Linux) is recommended for development._ π§ Linux - The shortcut is usually Ctrl + Alt + T. - Or search for Terminal in the application menu.ο£Ώ macOS (Mac) 1. Press Command (β) + Space to open Spotlight Search. 2. Type Terminal. 3. Press Enter, and you'll see a window with white text on black (or black text on white). πͺ Windows - Method 1 (CMD): Press Win + R, type cmd, and press Enter. This is the oldest command line. - Method 2 (PowerShell): Press Win + R, type powershell, and press Enter. This is a more modern and powerful terminal. - _Tip: Either works for simple tasks, but PowerShell or WSL (Windows Subsystem for Linux) is recommended for development._ π§ Linux - The shortcut is usually Ctrl + Alt + T. - Or search for Terminal in the application menu.
Practice makes perfect. Before diving into dry theory, let's get a feel for "typing commands" firsthand.Practice makes perfect. Before diving into dry theory, let's get a feel for "typing commands" firsthand.
> π‘ Tip: For safety and convenience, we recommend using the web simulator below. If you're confident, you can also open your computer's real terminal using the method from Chapter 0 and follow along (the experience is the same).> π‘ Tip: For safety and convenience, we recommend using the web simulator below. If you're confident, you can also open your computer's real terminal using the method from Chapter 0 and follow along (the experience is the same).
In this exercise, you will learn to:In this exercise, you will learn to:
ls or dir to see what's in the current directory.View files: Learn to use ls or dir to see what's in the current directory.mkdir to create new folders and cd to enter them like a portal.Create and navigate: Learn to use mkdir to create new folders and cd to enter them like a portal._Please select your operating system below, then follow the guided steps to begin:__Please select your operating system below, then follow the guided steps to begin:_
You might ask: _"Graphical interfaces (GUIs) are so easy to use β just click with the mouse. Why bother typing complex commands into a black-and-white window?"_You might ask: _"Graphical interfaces (GUIs) are so easy to use β just click with the mouse. Why bother typing complex commands into a black-and-white window?"_
This isn't about "acting like a hacker." It's because in certain situations, language (commands) is more powerful than gestures (mouse clicks).This isn't about "acting like a hacker." It's because in certain situations, language (commands) is more powerful than gestures (mouse clicks).
Summary: GUI is for exploration (browsing the web, viewing images), CLI is for production (development, operations, batch processing). As developers, we use the terminal because it is more precise, more controllable, and more efficient.Summary: GUI is for exploration (browsing the web, viewing images), CLI is for production (development, operations, batch processing). As developers, we use the terminal because it is more precise, more controllable, and more efficient.
_Different operating systems have different-looking terminals, and different command syntax. Click the buttons below to switch views β notice how macOS, Windows, and Linux use different commands (e.g., dir vs ls) to do the same thing:__Different operating systems have different-looking terminals, and different command syntax. Click the buttons below to switch views β notice how macOS, Windows, and Linux use different commands (e.g., dir vs ls) to do the same thing:_
Before graphical user interfaces (GUIs) became widespread, the terminal was the primary way humans interacted with computers. Even today, it remains the most precise and efficient tool for developers to control a computer.Before graphical user interfaces (GUIs) became widespread, the terminal was the primary way humans interacted with computers. Even today, it remains the most precise and efficient tool for developers to control a computer.
At its core, a terminal is a character stream input/output environment:At its core, a terminal is a character stream input/output environment:
It doesn't handle complex graphics, images, or video β it focuses on text-based interaction.It doesn't handle complex graphics, images, or video β it focuses on text-based interaction.
Before diving deeper, consider this question: Does the terminal window itself actually understand what you're saying?Before diving deeper, consider this question: Does the terminal window itself actually understand what you're saying?
In reality, the terminal is like a display that only relays messages. When you type the date command, the terminal doesn't know it means "show the date" β it just packages those 4 letters and sends them to the real boss behind the scenes: the Shell.In reality, the terminal is like a display that only relays messages. When you type the date command, the terminal doesn't know it means "show the date" β it just packages those 4 letters and sends them to the real boss behind the scenes: the Shell.
The Shell is the "brain" that can understand your commands and direct the computer to do the work.The Shell is the "brain" that can understand your commands and direct the computer to do the work.
To understand how they work together, let's look at these three components with clear roles. The best analogy for understanding their relationship is that of a web browser and a web server.To understand how they work together, let's look at these three components with clear roles. The best analogy for understanding their relationship is that of a web browser and a web server.
ls or cd means. It's like the Chrome browser β whether you visit Baidu or Google, it just renders the page.Characteristic: It has no intelligence of its own and doesn't understand what ls or cd means. It's like the Chrome browser β whether you visit Baidu or Google, it just renders the page.Because the display layer (terminal) and the logic layer (Shell) are completely separate, they can be freely combined:Because the display layer (terminal) and the logic layer (Shell) are completely separate, they can be freely combined:
You might think: _"I press 'a' on the keyboard, and the terminal draws 'a' on the screen."_You might think: _"I press 'a' on the keyboard, and the terminal draws 'a' on the screen."_
Wrong! The real process works like this (called Echo):Wrong! The real process works like this (called Echo):
> π‘ Mini experiment: Some commands (like when entering a password) disable the Shell's echo function. At that point, you press keys and the terminal sends them to the Shell, but the Shell doesn't send anything back, so the screen stays blank. This is a privacy protection measure.> π‘ Mini experiment: Some commands (like when entering a password) disable the Shell's echo function. At that point, you press keys and the terminal sends them to the Shell, but the Shell doesn't send anything back, so the screen stays blank. This is a privacy protection measure.
One-sentence summary of the flow:One-sentence summary of the flow:
You type in the terminal β‘οΈ signal goes to the Shell β‘οΈ Shell sends it back unchanged (you see the characters) and interprets it β‘οΈ Shell directs the kernel to do the work.You type in the terminal β‘οΈ signal goes to the Shell β‘οΈ Shell sends it back unchanged (you see the characters) and interprets it β‘οΈ Shell directs the kernel to do the work.
_The demo below illustrates this process. Watch the "wall" between the Shell and kernel, and how characters travel back and forth:__The demo below illustrates this process. Watch the "wall" between the Shell and kernel, and how characters travel back and forth:_
Unlike modern graphical interfaces that use "pixels," the terminal's display foundation is the character grid (Character Grid).Unlike modern graphical interfaces that use "pixels," the terminal's display foundation is the character grid (Character Grid).
The terminal screen is divided into rows and columns, and each cell is called a cell (Cell).The terminal screen is divided into rows and columns, and each cell is called a cell (Cell).
Each cell is the smallest unit of terminal display, containing two types of core information:Each cell is the smallest unit of terminal display, containing two types of core information:
A, δΈ, $).Glyph: The actual displayed character (e.g., A, δΈ, $).When you resize the terminal window, you're essentially changing the grid's rows and columns.When you resize the terminal window, you're essentially changing the grid's rows and columns.
_Try interacting with the area below and observe how the grid carries characters:__Try interacting with the area below and observe how the grid carries characters:_
The terminal cannot display images β all "interfaces" are achieved through combinations of character colors and styles.The terminal cannot display images β all "interfaces" are achieved through combinations of character colors and styles.
_Click on the cells below to see the style attributes behind each one:__Click on the cells below to see the style attributes behind each one:_
You might wonder: if the terminal only transmits text, how are colored text, moving cursors, and screen clearing achieved?You might wonder: if the terminal only transmits text, how are colored text, moving cursors, and screen clearing achieved?
The answer is escape sequences (Escape Sequences).The answer is escape sequences (Escape Sequences).
These are special character instructions (usually starting with the ESC character). When the terminal receives these characters, it doesn't display them on screen but instead interprets them as control commands.These are special character instructions (usually starting with the ESC character). When the terminal receives these characters, it doesn't display them on screen but instead interprets them as control commands.
For example:For example:
A β draw A on screen.Regular character A β draw A on screen.\033[31m β command: set subsequent text color to red.Sequence \033[31m β command: set subsequent text color to red.\033[2J β command: clear the screen.Sequence \033[2J β command: clear the screen.This is like an agreement with a friend: if I speak normally, you write it down; if I raise my left hand (equivalent to ESC), the next sentence is a command, not content.This is like an agreement with a friend: if I speak normally, you write it down; if I raise my left hand (equivalent to ESC), the next sentence is a command, not content.
_Click the "play" button below to watch the terminal process the character stream one by one and identify hidden commands:__Click the "play" button below to watch the terminal process the character stream one by one and identify hidden commands:_
_The component below shows more types of escape sequences and their rendering effects:__The component below shows more types of escape sequences and their rendering effects:_
The input process is often misunderstood. When you press a key, the terminal doesn't directly "draw" the character on screen β it performs an encoding transmission.The input process is often misunderstood. When you press a key, the terminal doesn't directly "draw" the character on screen β it performs an encoding transmission.
a β sends byte a.Pressing a β sends byte a.Up Arrow β sends sequence ^[[A.Pressing Up Arrow β sends sequence ^[[A.Key point: All key presses (including function keys and mouse clicks) are byte data at the transmission level.Key point: All key presses (including function keys and mouse clicks) are byte data at the transmission level.
_Try pressing keys below and observe how your input is converted to underlying data:__Try pressing keys below and observe how your input is converted to underlying data:_
The terminal has two very different "personalities." Understanding this helps you see why typing commands and playing Snake in the terminal are completely different experiences.The terminal has two very different "personalities." Understanding this helps you see why typing commands and playing Snake in the terminal are completely different experiences.
ls, cd).__Use case: Typing everyday commands (like ls, cd).__Click the buttons below to switch modes and experience the different "feel" of "writing a letter" vs "playing a game":__Click the buttons below to switch modes and experience the different "feel" of "writing a letter" vs "playing a game":_
Pressing Ctrl+C in the terminal usually stops a program. This isn't achieved by sending characters β it triggers a signal (Signal).Pressing Ctrl+C in the terminal usually stops a program. This isn't achieved by sending characters β it triggers a signal (Signal).
Signals are an operating system-level notification mechanism used to tell a program that a specific event has occurred.Signals are an operating system-level notification mechanism used to tell a program that a specific event has occurred.
SIGINT (Interrupt): notifies the program "please interrupt the current operation."Ctrl+C β sends SIGINT (Interrupt): notifies the program "please interrupt the current operation."SIGTSTP (Suspend): notifies the program "please pause and suspend to background."Ctrl+Z β sends SIGTSTP (Suspend): notifies the program "please pause and suspend to background."This mechanism bypasses the standard data input channel, ensuring users retain control even when a program is frozen.This mechanism bypasses the standard data input channel, ensuring users retain control even when a program is frozen.
Have you noticed that when you edit a file with vim or check system status with htop, they take over the entire screen? And when you exit, the screen instantly returns to its previous state with all your command history intact?Have you noticed that when you edit a file with vim or check system status with htop, they take over the entire screen? And when you exit, the screen instantly returns to its previous state with all your command history intact?
This is because the terminal has two "canvases" that switch back and forth:This is because the terminal has two "canvases" that switch back and forth:
_Click the button below to experience how the "scratchpad" and "blackboard" switch instantly:__Click the button below to experience how the "scratchpad" and "blackboard" switch instantly:_
------
The terminal is not a mysterious black box β it's a standardized text interaction interface.The terminal is not a mysterious black box β it's a standardized text interaction interface.
By understanding these underlying principles, you no longer need to blindly memorize commands β you can truly understand the logical flow behind every keystroke.By understanding these underlying principles, you no longer need to blindly memorize commands β you can truly understand the logical flow behind every keystroke.
| Term | English | Explanation |
|---|---|---|
| Terminal | Terminal | The window program responsible for display and input (front-end). |
| Shell | Shell | The program responsible for parsing commands and executing logic (back-end). |
| CLI | Command Line Interface | A text-based interaction method. |
| TUI | Text User Interface | A pseudo-graphical interface built with characters in the terminal. |
| Escape Sequence | Escape Sequence | Special character instructions used to control terminal cursor, colors, etc. |
| Standard Input/Output | Stdin/Stdout | Standard channels for programs to receive and output data. |