zenpoy
zenpoy

Reputation: 20136

How to spawn a new xterm with the same environment

I'm using virtualenv (which sets some environment variables). Now, I want to spawn a new terminal window that have the same environment. If I try:

xterm &

I get a new terminal but the environment is the default environment, that is when I enter the following line on the new terminal:

pserve --reload development.ini

I get:

>> pserve: Command not found.

On the other hand, if I execute:

xterm -e pserve --reload development.ini &

It opens a new terminal that runs pserve. So, my questions are:

  1. How to generally open a new terminal with the same environment
  2. How can the new terminal find pserve when I run it with -e switch?

Upvotes: 2

Views: 4467

Answers (2)

Davide
Davide

Reputation: 2264

Following the advice from @dmp I added to my ~/.bashrc the following:

# save the environment, apart from readonly variables that can not be restored
alias cloneterm='set |egrep -v "^(BASHOPTS|BASH_COMPLETION_COMPAT_DIR|BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID)=" > /tmp/env.tmp && $TERM &'
# restore a previously saved environment, if any
[ -f /tmp/env.tmp ] && source /tmp/env.tmp
[ -f /tmp/env.tmp ] && rm /tmp/env.tmp

Now I can simply run

$ cloneterm

and I get a new terminal window, with the same environment

Hope this helps

Notes:

  • this is bash-only but should work tith other terminals too (even f I only tested it on xterm)
  • this may break in multi-user systems, but is more than enough for desktop/laptop configurations

Upvotes: 1

dmp
dmp

Reputation: 439

To answer your first question, a quick and dirty way of doing it is to use the sh builtin 'set' command (more see 'help set').

From the old shell:

set > ~/env.tmp

Then

xterm &

From the new shell:

. ~/env.tmp && rm ~/env.tmp

You may want to wrap this up in a script or add a couple of functions in your 'bash.rc'. You may also want to use 'mktemp(1)' or similar.

Upvotes: 4

Related Questions