gpt4 book ai didi

linux - exec shell 命令的选项 -l

转载 作者:太空狗 更新时间:2023-10-29 11:42:09 25 4
gpt4 key购买 nike

能否请您澄清一下 exec shell 命令的 -l 选项的用法。我在运行 exec ls | 时没有发现任何差异catexec -l ls |猫

最佳答案

exec-l 选项在命令名称的开头添加一个-。例如:

exec -l diff | head

-diff: missing operand after '-diff'
-diff: Try '-diff --help' for more information.

注意 diff 之前的所有地方的 -

这一切的重点是什么?如果您在启动 shell 的命令前有 -,它将充当登录 shell。来自 man bash:

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

现在,man exec 指出:

If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does.

因此 exec -l bash 将作为登录 shell 运行 bash。为了测试这一点,我们可以使用登录 bash 执行文件 ~/.bash_profile 的事实,因此:

$ cat ~/.bash_profile 
#!/bin/sh

printf "I am a login shell!\n"

如果我启动登录 bash,命令 printf "I am a login shell!\n" 将被执行。现在使用 exec 进行测试:

$ exec bash
$

没有任何显示,我们在非登录 shell 上。

$ exec -l bash
I am a login shell!
$

这里我们有一个登录 shell。

关于linux - exec shell 命令的选项 -l,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39972978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com