2.1 First Conversation with Shell
Prerequisites: Chapter 1 (What Is a Computer Actually?)
Estimated time: 5 minutes
Exercise environment: The commands in this chapter are based on Bash 5 on GNU/Linux; differences with macOS and PowerShell will be addressed separately
You are ready to continue when: you can distinguish between terminal, Shell, built-in commands, and external programs
After the system survey in Chapter 1, focus on the terminal. A $ appears at the prompt. You do not need to write a complete program first; pass one line of text to the shell and it will locate the command, arrange its arguments, and start the program.
First, Recognize the Four Roles
- The terminal is responsible for input and output.
- The shell reads commands and processes parameters. Bash, Zsh, and PowerShell are all shells, but they have slightly different syntax.
- Built-in commands are executed by the shell itself, such as
cdin Bash or Zsh. - External commands are programs on the disk, such as
/usr/bin/whoamiin common systems.
The prompt often ends with $. The commands in the following code block do not include the prompt. Just copy the command text as is.
whoami
hostname
date
pwdThe output depends on your account, machine, time zone, and current directory. It does not need to match any specific username in the book.
Which Shell Is Currently Running
printf 'login shell: %s\n' "$SHELL"
printf 'current process: '
ps -p $$ -o comm=$SHELL is typically sourced from the account configuration, indicating the login shell; it does not guarantee to match the current process. You might start Zsh from Bash, or enter a container within a terminal.
Now, look at where the command is coming from:
type -a cd
type -a printf
type -a whoami
command -v whoamitype -a can display aliases, functions, built-in commands, and external commands. When troubleshooting, it's often more complete than which.
Commands, Options, and Parameters
printf '%s\n' 'hello terminal'Here, printf is the command, and '%s\n' and 'hello terminal' are two parameters. The shell first constructs the parameters according to the rules of quoting and expansion, then executes the command. Single quotes protect the whitespace inside, so the entire phrase remains a single parameter.
When you need help:
help cd # Bash built-in
man whoami # Common on Linux and macOS
whoami --help # Common on GNU/Linux; not supported on every platformAfter running these commands, look at the output of pwd. The next page uses it to answer a practical question: where does a relative path start?