user3282758
user3282758

Reputation: 1483

what is INTERACTIVE unix shell

I have been trying to understand what does it mean by 'interactive' shell. I have read a lot of material on net as well as gone thru the man pages - INVOCATION section.

So in particular I would like to put up some simple questions :

1) How do I start a non-interactive shell ? Is it by saying 'bash --login' ?

2) I just could not make out what additional could I do in an interactive shell that I can not do in a shell script (all this material keeps saying that shell script would run in a non-interactive shell)

3) When I start a terminal in a GUI, is the shell that runs there be an non-login interactive shell

4) can I run a shell script in an interactive shell

Upvotes: 2

Views: 396

Answers (1)

konsolebox
konsolebox

Reputation: 75548

As explained in the Bash manual:

An interactive shell is one started without non-option arguments and without the -c option whose standard input and error 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.

For your questions:

1) How do I start a non-interactive shell ? Is it by saying 'bash --login'?

Basically most of the time you can do it by running the shell with a script, or running the shell with the -c option. If the shell does not run with a script and if the shell gets its input redirected, the shell would automatically exit if the input ends.

2) I just could not make out what additional could I do in an interactive shell that I can not do in a shell script (all this material keeps saying that shell script would run in a non-interactive shell)

There isn't much difference only that the prompt is naturally not shown. Job control is also not enabled. It's only when Bash is executed interactively that it runs the rc file ~/.bashrc. Basically the shell would try to behave the way it should when not interacting with a terminal to make it work correctly and not hang somewhere else.

3) When I start a terminal in a GUI, is the shell that runs there be an non-login interactive shell

It certainly is an interactive shell at least by default.

4) can I run a shell script in an interactive shell

If you mean with the current shell itself, yes with . or source. The prompt returns when the shell script ends.

Upvotes: 3

Related Questions