1

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?

6
  • add at the end on script /bin/bash -l
    – kofemann
    Aug 14, 2013 at 6:49
  • Thanks, this indeed leaves a prompt with the env. vars set, except for those overridden by /etc/profile, or the user's .bashrc, such as PS1. How can PS1 be set for the new shell?
    – miluz
    Aug 14, 2013 at 7:10
  • Try just /bin/bash at the bottom. exec /bin/bash might work too.
    – Flup
    Aug 14, 2013 at 7:17
  • with options --noprofile --norc you will force bash not to read /etc/profile and bashrc. So add /bin/bash --noprofile --norc at the end of your script
    – kofemann
    Aug 14, 2013 at 7:30
  • 1
    This cause PS1 to be retained but loses the user's aliases set in .bashrc. I've switched from -l to -i and added --rcfile to workaround this problem. Now the script can do various kind of tasks, and calls for an rcfile to set env. vars and aliases for the user. This is the 'setup' kind of script I looked for, thanks!
    – miluz
    Aug 14, 2013 at 8:07

1 Answer 1

0

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
...
2
  • But you will have started a second bash shell? The user will have to logout twice at the end. I don't think you can that easily change the environment of the parent from the child...
    – Marki
    Aug 18, 2013 at 9:16
  • The intention is to open a new shell with modified aliases and environment variables. That way, when that environment is no longer needed, the users type 'exit' and resume their previous shell/environment.
    – miluz
    Aug 18, 2013 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .