Something About Bash
Bash
Bash is a command language interpreter that executes commands read from the standard input or from a file. We can use man bash
to check the detailed information about bash.
In this article, I am going to talk about how to use bash on Mac.
shell and subshell
To help understand shell and subshell, we can use brew install pstree
to install pstree on Mac.
As I use oh my zsh
on my Mac terminal, we can use bash
command to enter bash. And we can use pstree -s Terminal
to show running process in Terminal. Then we can enter a deeper layer of bash(which is so-called subshell) using bash
command. After that, we type pstree -s Terminal
again, and we can see the difference:
Actually, by using bash
command, we can enter any number of deeper layers, and we can use exit
command to quit the corresponding layer:
Executes command from a file
We can firstly edit a .sh
file:
Then we can enter bash and execute command from demo01.sh
. There are three ways to excute this file, and we should figure out the differences between these ways.
1. Using source or . command
The first way is to use source demo01.sh
or . demo01.sh
, which execute demo01.sh
in the current shell:
Note that echo $$
can check the id of current process, and we can easily notice that we execute the .sh
file in the currenct shell.
2. Using bash command
The second way is to use bash demo01.sh
, which opens a subshell to execute demo01.sh
and then quits the subshell:
3. Using chmod command
The third way is to use chmod +x demo01.sh
command, which can change demo01.sh
into an executable file. And then we can run this file directly:
However, by default, the third way still makes use of a subshell.