Shell - How Shell Works
Configuration file
~/.zshrc: The primary config file for zsh~/.bashrc: The primary config file for bash (on linux systems)
The shell automatically reads (it “sources”) this file every time you open a new terminal. The source command is a built-in shell command that executes a script in the current shell session. For example, if the script exports a variable (e.g., export MY_VAR=“hello”), that variable becomes part of your current shell’s environment.
How the shell executes programs
When a command is entered in the shell, it follows these steps to execute:
- Parsing: The shell reads the command line input and breaks it into components. The first word is the command (program), and the following words are arguments.
- Command Lookup: The shell searches for the command in the directories listed in the
$PATHenvironment variable.echo $PATH - Forking: The shell creates a new process (child process) to execute the command.
- Return: After the child process finishes, control returns to the user.
Executing programs
% echo $PATH
/Users/.../bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinThe echo $PATH command executes the program echo. The shell searches for it in the directories defined in $PATH. To find where the echo program resides:
% which echo
echo: shell built-in commandOther examples:
% which ls
/bin/ls
% which mkdir
/bin/mkdir
% which npm
/Users/.../bin/npm
% which python
/opt/anaconda3/bin/pythonCheck where is my python:
# There are two python3 installed on my commputer
% which -a python3
/opt/miniconda3/bin/python3
/usr/bin/python3
# But this command only return one
% which python3
/opt/miniconda3/bin/python3When you have multiple versions of the same command, The shell executes the first one it finds, searching the directories in your $PATH from left to right.
Export
In Linux, every time you run a command or a script, the system creates a process. The child process won’t see the variables defined in parent process unless declared using export.
# --worker.sh--
#!/bin/bash
echo "Starting worker in $APP_MODE mode on port $DB_PORT..."#!/bin/bash
export APP_MODE="production"
export DB_PORT=5432
./worker.shWithout export, the process running ./worker.sh won’t see the two variables.
Types of Commands
-
Built-in commands
A built-in command is a command that is part of the shell program itself. Examples:cd, echo, exit, export, alias, pwd.% type pwd pwd is a shell builtin -
External Commands
An external command is a separate program that exists as a file on your system. Examples:ls, grep, git, python, node.% type ls ls is /bin/ls
Advanced
Redirection
In the shell, each program has an input stream and an output stream — by default, the keyboard and terminal. We can redirect input or output using < file or > file.
% echo "Hello, World" > output.txtecho "Hello, World": prints text to the terminal.>: redirects output to the file specified on its right.
Result: the text “Hello, World” is written to output.txt. > overwrites a file, while >> appends to it.
Pipes
Pipes (|) pass the output of one command as input to another.
% ls -l | grep ".txt"
-rw-r--r-- 1 user staff 13 Jun 24 16:18 output.txtExplanation:
ls -l: lists files in long format.|: passes output to the next command.grep ".txt": filters for.txtfiles.
Combining redirection and pipes:
% ls -l | grep ".txt" > txt_files_list.txt
% cat txt_files_list.txt
-rw-r--r-- 1 user staff 13 Jun 24 16:18 output.txt
-rw-r--r-- 1 user staff 0 Jun 24 16:34 txt_files_list.txtUseful
Alias
bash-3.2$ alias gs="git status"
bash-3.2$ gs
On branch main
nothing to commit, working tree clean
bash-3.2$ git status
On branch main
nothing to commit, working tree cleanTo make an alias persistent, include it in configuration files like .bashrc or .zshrc.