gpt4 book ai didi

linux - docker ubuntu 容器 : shell linked to bash still starts shell

转载 作者:太空狗 更新时间:2023-10-29 11:17:59 26 4
gpt4 key购买 nike

好的伙计们,所以我尝试在基于 ubuntu:14.04 的 docker 容器中安装 rvm。在这个过程中,我发现有些人做这样的事情来确保 docker 命令也与 bash 一起运行:

RUN ln -fs /bin/bash /bin/sh

现在奇怪的事情发生了,我希望你们有人能给我解释一下:

→ docker run -it --rm d81ff50de1ce /bin/bash
root@e93a877ab3dc:/# ls -lah /bin
....
lrwxrwxrwx 1 root root 9 Mar 1 16:15 sh -> /bin/bash
lrwxrwxrwx 1 root root 9 Mar 1 16:15 sh.distrib -> /bin/bash
...
root@e93a877ab3dc:/# /bin/sh
sh-4.3# echo $0
/bin/sh

有人能解释一下这是怎么回事吗?我知道我可以在 dockerfile 中为我的命令加上前缀 w/dockerfile 中的前缀。

非常感谢,罗宾

最佳答案

这是因为 bash 有一个兼容模式,如果它是通过名称 sh 启动的,它会尝试模拟 sh,如联机帮助页所述:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.

关于linux - docker ubuntu 容器 : shell linked to bash still starts shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35728388/

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