Shell
Shell
sh
(or the Shell Command Language) is a programming language described by the POSIX standard. It has many implementations (ksh88
, dash
, …).
bash
can also be considered an implementation of sh.
Command
#!/bin/sh
will use sh (and whatever that happens to point to). For a long time, it is a symlink or hardlink to /bin/bash
by default in many OS.
#!/bin/bash
will use /bin/bash
if it’s available (and fail with an error message if it’s not).
Of course, you can also specify another implementation, e.g. #!/bin/dash
Execution
source
$ source a.sh
$ . a.sh # There is space between . and a.sh
Execute in current shell and keep environment change from a.sh.
sh/bash
$ sh a.sh
$ bash a.sh
Execute in a subshell and environment does not change in parent shell.
./
./a.sh
Need permission to execute in a subshell and environment does not change in parent shell.
fork、source、exec
./a.sh fork
./a.sh source
./a.sh exec
fork
creates a child process to execute a.sh and will not change the current environment after execution.
source
executes a.sh in current process and will change the environment according to a.sh.
exec
is similar to source but will terminate current process.
By default, fork
is applied.
竟然无法拒绝你的打赏