How to write a shell script that will reload terminal at middle of the script and then start using commands again?

pk flag

I recently knew about sdkman in and I wanna install different java versions using this manager. But I have a very old version of sdkman. That's I need to update it, then install java. But I don't wanna type this commands in my every repository in gitpod. That's why I have written an shell script and I will use this file in every repository. The script is:-

sdk update
sdk install java 17-open

# there are lots of command under this, but they're not necessary in this question.

But then I knew that after updating sdkman, I need to restart terminal. I found that exec bash restarts terminal. But when I add exec bash in my shell script, it doesn't execute the commands below the exec bash command. Can someone suggest me, how to do this?

sdk update
exec bash #recently added, but doesn't execute the commands after it :(
sdk install java 17-open
rajin100000 avatar
pk flag
How can it help me??
cn flag

The problem you are having is when you open a new shell or restart the current shell, your script will no longer be loaded in the new shell. So, what you need to do is:

  1. Open the parent shell
  2. Execute the first command
  3. Create a child shell within the parent script
  4. This child shell will be loaded with the new environment parameters
  5. Execute the second command within the child shell
  6. Exit child shell
  7. Exit parent shell

For the above mentioned commands, you can use something like this.

sdk update
sudo su - $USER -c 'sdk install java 17-open'

The first command will run in the parent shell. The second line (with sudod su - $USER) will open a new child shell within the same script, and execute the second command.

PS: You will be asked to enter the password, if you haven't configured "NOPASSWD" for sudo. Check this to setup that.


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.