$ echo $0
-bash
$ dash
$ echo $0
dash
$ exit
$ echo $0
-bash
| 选项 | 描述 |
|---|---|
| -c string | 从 string 中读取命令并进行处理 |
| -i | 启动一个能够接收用户输入的交互式 shell |
| -l | 作为登录 shell 启动 |
| -r | 启动一个受限 shell,将用户限制在默认目录中 |
| -s | 从标准输入中读取命令 |
$ pwd ; ls test* ; cd /etc ; pwd ; cd ; pwd ; ls my*
$ (pwd ; ls test* ; cd /etc ; pwd ; cd ; pwd ; ls my*)
在交互式 shell 中, 一种高效的子 shell 用法是后台模式。
sleep 命令会接受一个参数作为希望进程等待(睡眠)的秒数。该命令在 shell 脚本中常用于引入一段暂停时间。命令 sleep 10 会将会话暂停 10 秒,然后返回 shell CLI 提示符。
$ sleep 3000&
[1] 2542
$ ps -f
UID PID PPID C STIME TTY TIME CMD
christi+ 2356 2352 0 13:27 pts/0 00:00:00 -bash
christi+ 2542 2356 0 13:44 pts/0 00:00:00 sleep 3000
christi+ 2543 2356 0 13:44 pts/0 00:00:00 ps -f
$ jobs
[1]+ Running sleep 3000 &
$ jobs -l
[1]+ 2542 Running sleep 3000 &
$
[1]+ Done sleep 3000
通过将进程列表置入后台,可以在子 shell 中进行大量的多进程处理。由此带来的一个好处是终端不再和子 shell 的 I/O 绑定在一起。
$ (tar -cf Doc.tar Documents ; tar -cf Music.tar Music)&
[1] 2567
$
$ ls *.tar
Doc.tar Music.tar
[1]+ Done ( tar -cf Doc.tar Documents; tar -cf Music.tar Music )
$
$ coproc sleep 10
[1] 2689
$ jobs
[1]+ Running coproc COPROC sleep 10 &
$ coproc My_Job { sleep 10; }
$ jobs
[1]+ Running coproc My_Job { sleep 10; } &
$ alias li='ls -i'