Home > Back-end >  What is the difference between "bash -i myscript.sh" vs "bash myscript.sh"?
What is the difference between "bash -i myscript.sh" vs "bash myscript.sh"?

Time:01-21

According to bash man page, it says -i is for interactive mode of shell. I tried example code to find out what -i option does. interactive.sh is script that needs user input, which means interactive script.

The default bash option is non-interactive mode. But the interactive.sh runs without any problem with non-interactive mode. It also runs well with interactive mode. It confuses me.

What is the exact usage of -i option in bash? What is the difference between interactive and non-interactive mode in 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

CodePudding user response:

With bash -i script you are running interactive non-login shell.q

What is the exact usage of -i option in bash?

From man bash:

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

What is the difference between interactive and non-interactive mode in shell?

There are some differences. Look at 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.

etc. For example bash -i -c 'echo $PS1'.

  • Related