How to run a new shell with copy of current shell declarations?

How to run another shell and have all current shell dectaration copied to that new shell?

I would like to have available all current declarations in a new shell. That are functions, aliases, variables.

I need to test some functions that use the 'exit', but running it in current shell on 'exit' will close the shell, which I do not like.

# export thing=hello
# echo $thing
hello
# bash
# echo $thing
hello
#

I don't see the problem...

I had a similar issue recently and found that I could use the "return" command and it wouldn't kill my session.
ex:
return 10

This is under ksh.

The problem is:
The only EXPORTED variables are copyed.
I need many functions and declared not exported variables.
I am running functions which change and define variables.
Some of them need to be run in separate process, in '()': like >(funct_boo)
I do not want redeclare all functions, aliases and variables in the new process

src> alias boo="echo boo-boo"
src> foo()
> {
> echo "foo from func";
> }
src> foo
foo from func
src> boo
boo-boo
src> var=variable
src> echo $var
variable
src> hm
dca0701> bash
executing file .bashrc - starting the bash shell--
----> Start of the .myset file  <---
----> End   of the .myset file  <---
src> foo
bash: foo: command not found
src> boo
bash: boo: command not found
src> echo $var

src>

The return() I have used, but it is not cover my situation.
There is a stack of functions, if somewhere something has not-0-return I need to stop execution of all stacked function, but do not kill the shell.
Return works only for one function.

Is any other possibility exist?

Still have no idea how to do it.
Is it possible to run another shell and have all FUNCTIONS and ALIASES from current shell be available in new one?

bash has an -a option to automatically export all variables and functions, dunno if this is a bashism but I would guess it's POSIX. Try "set -a" at the top of the script.

In bash, it seems you can't export aliases. All the more reason to use functions instead of aliases.

Thank you, ERA, for advise, but I could not have it work.
Not sure where 'set -a' should be used:

  • in original shell, where function and variables are defined and where new shell is started
  • whith start shell command, like : >bash -a
  • in new shell after starting

I have tried all 3 way but do not have any success.

--193:/home/dca0701/develop/src> alias boo="echo boo-boo"
--193:/home/dca0701/develop/src> foo()
> { echo "foo from func";  }
--195:/home/dca0701/develop/src> var=variable
--195:/home/dca0701/develop/src> ec $var; boo; foo
variable
boo-boo
foo from func


--196:/home/dca0701/develop/src> bash -a
executing file .bashrc - starting the bash shell--

----> Start of the .myset file  <---
----> End   of the .myset file  <---
--193:/home/dca0701/develop/src> ec $var; boo; foo

bash: boo: command not found
bash: foo: command not found
--193:/home/dca0701/develop/src> exit


--197:/home/dca0701/develop/src> set -a
--198:/home/dca0701/develop/src> bash
executing file .bashrc - starting the bash shell--

----> Start of the .myset file  <---
----> End   of the .myset file  <---
--193:/home/dca0701/develop/src> ec $var; boo; foo

bash: boo: command not found
bash: foo: command not found
--193:/home/dca0701/develop/src> exit


--199:/home/dca0701/develop/src>
--199:/home/dca0701/develop/src>
--202:/home/dca0701/develop/src> bash
executing file .bashrc - starting the bash shell--

----> Start of the .myset file  <---
----> End   of the .myset file  <---
--194:/home/dca0701/develop/src> set -a
--195:/home/dca0701/develop/src> ec $var; boo; foo

bash: boo: command not found
bash: foo: command not found
--196:/home/dca0701/develop/src>

Possible but not easily done. Think about it, opens up all sorts of security issues.

BTW, most test suites use a combination of TCL and expect to test shell scripts.

How about:

$ declare > my_declarations
$ bash
$ source my_declarations

I'm not sure that's what you're looking for, but it might work... dunno

*EDIT:
no... don't even bother... declare doesn't store alias declarations

Works for me, but I guess you will have to set -a before you declare the ones you want exported.

vnix$ PS1='nst> '
nst> set -a
nst> fnord=foo
nst> foo () { echo foo; }
nst> alias bar='echo bar'
nst> bash
vnix$ echo "$fnord"
foo
vnix$ foo
foo
vnix$ bar
bash: bar: command not found
vnix$ exit
nst> exit