2

It seems that either I don't understand what these options mean, or bash behaves the same (as interactive shell that reads standard input) without any options anyway. When would I want to use these options explicitly?

  • 5
    Reading INVOCATION part of man bash would be useful – Esref Jul 11 '15 at 10:37
  • 3
    Did you read the man page? What part you don't understand? – Braiam Jul 11 '15 at 10:57
  • I asked this question exactly after reading the man page, and I described what I don't understand in the question. – Max Yankov Jul 11 '15 at 10:59
2

From what I can understand it seems to be used for testing purposes. A startup file can use this to test the state as well as a shell script.

By default when you invoke a bash shell, it uses -i and -s so I am assuming for testing purposes you can invoke the shell explicitly with these options via the script or file to test the state that a normal login bash shell would provide.

I found that information from the man page under invocation Esref stated in the comments. It may not be so easily understood though, as it doesnt seem to be something a person would do normally.

| improve this answer | |
  • "you can invoke the shell explicitly with these options via the script or file to test the state that a normal login bash shell would provide" — but I can also just run bash and get exactly the same behavior as I would with these flags, so it still doesn't make sense to use them explicitly. – Max Yankov Jul 11 '15 at 11:31
  • Only via a script to test that its working as you intend. In a lot of cases people add debugging info and other stuff in a script and automate the testing part. EDIT: Think of it another way, they can write a script that runs the bash shell explicitly and then test it against hundreds of different systems automatically, instead of logging in manually to every system to test. – dakka Jul 11 '15 at 11:35
5

A different way to look at Bash invocation

The reference manual has a pretty detailed description on how to invoke Bash. I prefer to document it slightly differently though:

bash <options> <script_file> [<arguments>]
bash <options> -c <command_string> [<arguments>]
bash <options> [-s [<arguments>]

The first two are non-interactive, and hence don't have the associated features (prompt, job control, command history,...); However, if you need those features, you can enforce them with the -i flag.

The third one is interactive, and the optional -s flag is justified in the paragraphs below...

Specifying arguments

Example 1: Basic

The -s option can be used to specify arguments:

bash -s a b c

starts a shell... In this shell we can check arguments:

echo $1 $2 $2

reflects the arguments from the bash command:

a b c

Without the '-s' option, the first positional argument would be interpreted as a file name:

bash a b c

gives

bash: a: No such file or directory

Example 2: Using a heredoc

The previous use of the -s option can be used to provide arguments to a heredoc (when you don't want to use a file to embed the commands):

bash -s a b c <<'EOC'
echo $1 $2 $3
EOC

gives:

a b c

Note that the bash command executes the commands in the heredoc, and then exits; This brings you back to the shell where you invoked the bash command.


Example 3: Pipelining

Consider embedding bash in a pipeline (when you need to implement some inline scripting), again the opportunity to provide arguments can be exploited

echo 'echo HELLO $1' | bash -s WORLD

gives:

HELLO WORLD

This is a contrived example, but for some more complex scripting (with a heredoc!), this can come in handy...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.