Linux – bash: setting environment variables from a script and leave a shell prompt

bashlinux

I'm looking for a way to create some kind of a 'setup' script that would set environment variables and at the end leave the user with a bash command prompt.

for example:

#!/bin/bash
export PATH=...
export LD_LIBRARY_PATH=...
export PS1=...
...
<give a shell prompt with the above environment variables set>

I don't want to source a file with the environment variable settings since I'd like the user to be able to 'exit' back to the shell where this 'setup' was launched.

How can this be done?

Best Answer

Thanks for all the comments and help, I'll sum up the answer:

The setup script is built from two parts. The first part is a script which needs to do various kind of tasks. The second part is a rc file which contains the environment variables and aliases.

That way the environment script can have logic and can abort setup if some conditions are not met, and the rc file is used to set environment variables which are not overriden by /etc/profie or .bashrc.

Bash is called in interactive mode, and the rc file is sourcing .bashrc so the users can retain their existing aliases and environment variables.

Main script:

#!/bin/bash
... setup logic ...
# final call to bash for login prompt
exec "/bin/bash --rcfile /path/to/rcfile -i"

The rc file:

# source the user's bashrc
source ~/.bashrc

# environment variables
export PS1="\e[1;33m\u@\h \w> \e[m"

# aliases
...
Related Topic