Shell

Basic

What is the shell?

A shell is a user interface that provides access to various services of an operating system. The shell is a command-line interface. The shell of macOS before Catalina is Bourne Again Shell (bash). The shell allows users to execute commands, run programs, and manage system resources. The GUI has defined many use cases for us to interact with the operating system, however, to take full advantage of the tools our computer provides, we have to use the shell.

Basic shell commands

  • .: current directory
  • ..: parent directory
  • ls
  • madir
  • cd : change directory
  • pwd: print working directory
  • echo “text”: prints the specified text to the terminal
  • cat : displays the contents of the specified file
  • touch example.txt: creates file
  • grep “Hello” example.txt: To search for the string “Hello” in a file named example.txt, return the line containing the string

How the shell executes programs

When a command is entered in the shell, it follows these steps to execute the program:

  1. Parsing: The shell reads the command line input and breaks it into individual components (splitting by space). The first word is the command (program), and the folowing words are arguments.
  2. Command Lookup: The shell searches for the command in the directories listed in the PATH environment variable. We can view the PATH variable by executing echo $PATH.
  3. Forking: The shell creates a new process (child process) to execute the command.
  4. Return: After the child process finishes, the shell returns control to the user.

Executing programs

1
2
(base) zhaosonglin@xiaozeimaodeMacBook-Pro ~ % echo $PATH
/Users/zhaosonglin/.nvm/versions/node/v16.20.2/bin:/usr/local/mysql/bin:/opt/anaconda3/bin:/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Library/TeX/texbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/bin

The echo $PATH command essentially executes the program echo. The program named echo is searched through the directories contained in variable $PATH. We can find where the echo program resides by executing which echo.

1
2
(base) zhaosonglin@xiaozeimaodeMacBook-Pro ~ % which echo
echo: shell built-in command

The command echo is a shell built-in command. Other command examples:

1
2
3
4
5
6
7
8
% which ls
/bin/ls
% which mkdir
/bin/mkdir
% which npm
/Users/zhaosonglin/.nvm/versions/node/v16.20.2/bin/npm
% which python
/opt/anaconda3/bin/python

The commands like ls and mkdir resides in the default directory /bin/, while npm and python are installed in specific locations.

Advanced

Redirection

In the shell, each program has one input stream and one output stream, which are keyboard and terminal respectively by default. We can redirect the input or output by < file or > file. This is useful for saving the output for later use or for further processing.

1
% echo "Hello, World" > output.txt
  • echo "Hello, World": Prints “Hello, World!” to the terminal.
  • >: The redirection operator takes the output of the command on its left and writes it to the file specified on its right.

As a result, the text “Hello, World” is written to the file output.txt. We can check by the cat command:

1
2
% cat output.txt
Hello, World

> is used to overwrite the contents of a file, while >> is used to append to the contents of a file.

Pipes

Pipes | are used to pass the output of one command as input to another command.

1
2
% ls -l | grep ".txt"
-rw-r--r-- 1 zhaosonglin staff 13 Jun 24 16:18 output.txt
  • ls -l: Lists files and directories in long format.
  • |: The pipe operator takes the output of ls -l and passes it as input to the next command.
  • grep ".txt": Filters the input and displays only lines containing .txt.

As a result, the command finds only files ends with “.txt” of the output of ls -l. An example of combining redirection and pipes:

1
2
3
4
% ls -l | grep ".txt" > txt_files_list.txt
% cat txt_files_list.txt
-rw-r--r-- 1 zhaosonglin staff 13 Jun 24 16:18 output.txt
-rw-r--r-- 1 zhaosonglin staff 0 Jun 24 16:34 txt_files_list.txt

Useful

Alias

1
2
3
4
5
6
7
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 you need to include it in dotfiles like .bashrc or .zshrc.


Shell
https://thiefcat.github.io/2024/06/24/MIT-Missing/Shell/
Author
小贼猫
Posted on
June 24, 2024
Licensed under