"bash -i myscript.sh"与"bash myscript.sh"有什么区别?



根据bash手册页,它说-i是shell的交互模式。我尝试了示例代码来找出-i选项的作用。.sh是需要用户输入的脚本,即交互式脚本。

默认的bash选项是非交互模式。但是在非交互模式下,interactive.sh的运行没有任何问题。它在交互模式下也运行良好。这让我很困惑。

-i选项在bash中的确切用法是什么?shell中交互模式和非交互模式的区别是什么?

$cat interactive.sh
#!/bin/bash
echo 'Your name ?'
read name
echo "Your name is $name"
$ bash interactive.sh
Your name ?
ABC
Your name is ABC
$ bash -i interactive.sh
Your name ?
DEF
Your name is DEF

使用bash -i script,您正在运行交互式非登录shell。q

bash中-i选项的确切用法是什么?

Fromman bash:

-i        If the -i option is present, the shell is interactive.

shell中交互模式和非交互模式的区别是什么?

有一些不同。看man bash | grep -i -C5 interactive | less:

An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and er‐
ror  are  both  connected to terminals (as determined by isatty(3)), or one started with the -i option.  PS1 is set and $- includes i if bash is
interactive, allowing a shell script or a startup file to test this state.
When an interactive login shell exits, or a non-interactive login shell executes the exit builtin command, bash reads and executes commands from
the file ~/.bash_logout, if it exists.
When  an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.  This may
be inhibited by using the --norc option.  The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

[…]

When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not kill an interactive  shell),  and  SIGINT  is
caught  and handled (so that the wait builtin is interruptible).  In all cases, bash ignores SIGQUIT.  If job control is in effect, bash ignores
SIGTTIN, SIGTTOU, and SIGTSTP.

等。例如:bash -i -c 'echo $PS1'.

最新更新