1

I am trying to write a script that sets my main terminal to use bash shell. I know the #!/bin/bash will call all the commands in the script to be run with the bash shell but what I want is a script that specifically that changes the shell of my terminal to bash.

for example: (this is how my terminal looks like when it is opened.)

$

when i want to set the terminal to bash I manually type the bash command and press enter.

$bash

outcome:

[tyg@rooto ~]$

The problem is if I write a script using the above command it works but any command after the bash command in the script fails to execute. for example

#!/bin/bash
bash
echo "setting terminal environment to bash"
echo "success"

output:

[tyg@rooto ~]$ 

Expected output: (something like this)

[tyg@rooto ~]$ setting terminal environment to bash
[tyg@rooto ~]$ success

or (Like this)

[tyg@rooto ~]$
[tyg@rooto ~]$ setting environment
[tyg@rooto ~]$ success

any of the above is what I assume should be expected. Why are the two echo commands in the script failing to execute and is there a fix to this. Thanks

African_king
  • 177
  • 1
  • 8
  • best to **edit your question** and explain why you have both `#!/bin/bash` and then `bash`. Normally, you don't need the 2nd one. Are you trying to do somethi8ng more elaborate than your posted code? Good luck~! – shellter Jul 24 '20 at 02:30
  • @African_king : Your question is not about programming, and as such should better be asked at [superuser](https://superuser.com/), but as a general idea: Virtually every terminal emulator can be configured which shell to use, and once you are inside a shell, you can always either replace the shell by a different one (using `exec`) or create a different shell as a child process, in which you continue to work. – user1934428 Jul 24 '20 at 06:56
  • @shellter : I am trying to incorporate this into an ssh login I also added more details to the question if that okay – African_king Jul 24 '20 at 11:20
  • @user1934428 : yh thanks I just checked out super user – African_king Jul 24 '20 at 11:20

2 Answers2

1

#!/bin/bash will invoke the shell in which the subsequent script commands will be executed and upon completion, the shell will be gone. This is how you "execute commands in the changed shell"

bash on line 2 opens a 2nd bash shell in which your echo command is being executed. It too goes away on completion of the script - that's why you don't see the output as expected.

Yes, you can execute a "bash" script in any shell as long as the first line has the correct shell you want.

By removing the bash on line two, you'll see your output.

belwood
  • 3,320
  • 11
  • 38
  • 45
-1

Example 1

#!/bin/bash

bash << "EOT"
echo "setting environment"
EOT

Example 2

Add this code to a sample script, test.sh, then launch it:
#!/bin/bash 

echo "Shell: $$"
bash << "EOT"
echo "Shell: $$"
EOT
echo "Shell: $$"

Result:

Shell: 569 # Current shell
Shell: 570 # Shell in your new enviroment
Shell: 569 # Back to old shell
wuseman
  • 1,259
  • 12
  • 20