0

I'm trying to run a simple program

#!/bin/bash
bash
echo "Hello World"

But the program only gets as far as the 'bash' command on line 2. Why can't the program run line 3? The program terminates at the bash input

bash-3.2$

Is there another line I need to add so the program can print out "Hello World" ? Keep in mind I do understand I am already in a bash shell, but I still don't understand this error.

closed as unclear what you're asking by jasonwryan, muru, Anthon, Archemar, cuonglm Oct 7 '15 at 6:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    It's not terminated. It's just waiting for you to do something with the bash shell the script started. What exactly do you want to achieve? – muru Oct 6 '15 at 23:06
  • I'm not looking to do anything specific really, I just wanted to know why it wont echo Hello World and what I can fix so it will echo Hello World – shortcircuit Oct 6 '15 at 23:12
  • Remove the bash. – muru Oct 6 '15 at 23:12
  • You could have bash actually run something, eg bash -c "echo why?" – DarkHeart Oct 6 '15 at 23:17
  • 3
    Line 3 doesn't run because it won't run until the bash you started on line 2 terminates. bash is no different to any other program you might run on line 2....line 3 won't run until the program on line 2 finishes (unless you cause it to run in the background with &) – cas Oct 6 '15 at 23:27
2

Assuming that you're running this from a command prompt in a terminal, you're running three instances of bash:

  1. The interactive bash running in the terminal.
  2. The bash instance that runs your script.
  3. The bash instance that is invoked by the second line of the script.

In your script, you invoke bash with no argument, so it reads commands from its standard input. Since the input is coming from a terminal, you get an interactive shell, that prints a prompt and so on.

Once you exit from instance #3, the script (instance #2) continues to the next line and prints Hello World. After this the script terminates and you're back to your original command prompt (instance #1).

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