Ran*_*ane 4 linux shell command
您能否澄清一下shell命令-l选项的使用exec。当我跑exec ls | cat和exec -l ls | cat.
该-l的选择exec增加了一个-在你的命令的名称的开头。例如:
exec -l diff | head
-diff: missing operand after '-diff'
-diff: Try '-diff --help' for more information.
Run Code Online (Sandbox Code Playgroud)
注意-之前的无处不在diff。
这一切的重点是什么?如果你有一个-before 命令来启动一个 shell,它将充当一个登录 shell。来自man bash:
登录 shell 是参数零的第一个字符是 - 或以 --login 选项开头的 shell。
现在,man exec声明:
如果提供了 -l 选项,shell 会在传递给 command 的第零个参数的开头放置一个破折号。这就是 login(1) 所做的。
所以exec -l bash将bash作为登录shell运行。为了测试这一点,我们可以使用登录 bash 执行文件的事实~/.bash_profile,因此:
$ cat ~/.bash_profile
#!/bin/sh
printf "I am a login shell!\n"
Run Code Online (Sandbox Code Playgroud)
如果我启动登录 bash,printf "I am a login shell!\n"将执行该命令。现在测试exec:
$ exec bash
$
Run Code Online (Sandbox Code Playgroud)
什么也没有显示,我们在一个非登录的 shell 上。
$ exec -l bash
I am a login shell!
$
Run Code Online (Sandbox Code Playgroud)
这里我们有一个登录shell。