Bash options and interactive shells

bash

The -s option to bash doesn't seem to really do anything

When I execute commands with or without it nothing changes. What really confuses me is when I execute a shell script I always seem to be getting an interactive shell. I'm assuming it's interactive because when I use the read builtin in the script it will always prompt me for input. Does this make it interactive?

Do shell script run as interactive in fedora, and are there any examples of a difference that the -s can make? I have read the man pages, but can't seem to generate any examples on my own that would have any effect. I combined the two questions because I was trying to use s to change how the script received input, and in some tutorials they say it has an effect. I realize that it can set arguments, what I don't get is how it changes it to read from standard input it always seems to do that anyway

Here is what I used to test it

if [ -v $PS1 ]
then
  echo non-interactive
else
  echo interactive
fi
read ; echo $REPLY

read was always able to work in both non and interactive shells

Even when I tested for the presence of fd/0 and fd/1 in non-interactive shells they still existed

Thanks in advance

Best Answer

Bash will also determine whether or not it is interactive by examining if its input/output are attached to a terminal.

The -s non-interactive switch allows the bash script to process positional parameters when reading commands from a file. eg:

$ cat demo.sh
echo '$0 = ' $0
echo '$1 = ' $1
echo '$2 = ' $2

$ bash < demo.sh foo bar
bash: foo: No such file or directory

$ bash -s < demo.sh foo bar
$0 =  bash
$1 =  foo
$2 =  bar