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:

  1. 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.
  2. Command Lookup: The shell searches for the command in the directories listed in the $PATH environment variable.
    echo $PATH
  3. Forking: The shell creates a new process (child process) to execute the command.
  4. 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:/sbin

The 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 command

Other examples:

% which ls
/bin/ls
% which mkdir
/bin/mkdir
% which npm
/Users/.../bin/npm
% which python
/opt/anaconda3/bin/python

Check 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/python3

When 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.sh

Without 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.txt
  • echo "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.txt

Explanation:

  • ls -l: lists files in long format.
  • |: passes output to the next command.
  • grep ".txt": filters for .txt files.

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.txt

Useful

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 clean

To make an alias persistent, include it in configuration files like .bashrc or .zshrc.


Shell - How Shell Works
http://example.com/2024/06/24/CS_Basis/Shell2/
Author
Songlin Zhao
Posted on
June 24, 2024
Licensed under