来自https://unix.stackexchange.com/a/276611/674
当
bash
与 一起运行时-c
,它被认为是一个非交互式 shell~/.bashrc
,除非-i
指定,否则它不会读取。所以,Run Code Online (Sandbox Code Playgroud)$ type cp cp is aliased to ‘cp -i’ # Defined in ~/.bashrc $ cp .file1 file2 cp: overwrite ‘file2’? n $ bash -c "cp .file1 file2" # Existing file is overwritten without confirmation! $ bash -c -i "cp .file1 file2" cp: overwrite ‘file2’? n
shell 是由bash -i -c <command>
交互式还是非交互式创建的?
这样的 shell 不接受来自 stdin 的命令,是吗?所以它不是交互式的,是吗?
这样的 shell 读取~/.bashrc
,所以它不能是非交互式的,对吗?
来自Bash 参考手册:
要在启动脚本中确定 Bash 是否以交互方式运行,请测试“-”特殊参数的值。当 shell 是交互式的时,它包含 i 。
对于这个例子,
$ bash -c 'echo $-' # This is a non-interactive shell
hBc
$ bash -i -c 'echo $-' # This is an interactive shell
himBHc
Run Code Online (Sandbox Code Playgroud)