我知道这非常罕见,而且是个概念,但想不出它的用途。我确实有一个想法,我们可能想要执行一个脚本,其中包含通过 ssh 登录到服务器的命令,这是有时需要做的事情吗?这是非交互式登录 shell 有用的一个例子吗?
答案1
研究 Unix 命令的运行方式:现有(登录)shell 被分叉(复制),然后执行命令。如果分叉的 shell 运行另一个 shell,则该 shell 是非交互式的。输入命令“
$ echo "$- 'i' indicates interactive"
himBHs 'i' indicates interactive
现在将上面的行放入文件(foo)并运行
bash foo
hB 'i' indicates interactive
没有“i”,因此在非交互式 shell 中运行。使 foo 可执行并重试”
chmod +x foo
./foo
hB 'i' indicates interactive
同样,没有“i”,除非你强迫事情:
bash -i foo
himBH 'i' indicates interactive
然后你又看到了“i”