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:
- 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.
- 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
. - Forking: The shell creates a new process (child process) to execute the command.
- Return: After the child process finishes, the shell returns control to the user.
Executing programs
1 |
|
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 |
|
The command echo
is a shell built-in command. Other command examples:
1 |
|
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":
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 |
|
>
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 |
|
ls -l:
Lists files and directories in long format.|:
The pipe operator takes the output ofls -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 |
|
Useful
Alias
1 |
|
To make an alias persistent you need to include it in dotfiles like .bashrc or .zshrc.