Making a bash script switch to interactive mode and give a prompt

There are several options:

  • You start script in the same shell, using source or .;
  • You start a new shell but with your script as a initialization script:

The first is obvious; I write a little bit more details about the second.

For that, you use --init-file option:

bash --init-file my-init-script

You can even use this option in the shebang line:

#!/bin/bash --init-file

And then you start you script as always:

./script-name

Example:

$ cat ./script-name
#!/bin/bash --init-file
echo Setting session up
PS1='.\$ '
A=10
$ ./script-name
Setting session up
.$ echo $A
10
.$ exit
$ echo $A

$

As you can see, the script has made the environment for the user and then has given him the prompt.

Tags:

Bash