linuxbashscriptingsuperuser

Can I change my terminal environment to bash shell using a script and then execute other commands after changing the shell in the same script?


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


Solution

  • #!/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.