Say I login using bash and I define an alias.

$ alias c=clear
$ echo $0

Now I enter dash.

$ dash
$ echo $0

The alias defined in bash isn't available:

$ alias

How do I access it? More generally how do I access stuff in memory from the child process?

To be more precise, what I mean with "access" is being able to get the same data that I get when I run alias in the parent process.


More generally you don't access data from another process. It is possible to setup shared memory sections which, as the name suggests, are shared between multiple processes. I don't believe these are available from any shell like bash or dash. But normal memory cannot be accessed by another process. This is done deliberately for security.

However you can set environment variables which will be inherited by the child process as it is created. In bash you export a variable to do this:

export x

echo $x
  • Yes you can access all the memory of another process (whether "normal" or not), provided that they're run by the same user. That's what /proc/[pid]/mem is for. And shellshock was caused by a bug when parsing the functions encoded in envvars -- exporting functions in the environment is still very much allowed in bash. – pizdelect Apr 10 at 15:33
  • Yes, I've tried it. Notice that you can both read and write to /proc/[pid]/mem. There's also process_vm_writev, etc. – pizdelect Apr 10 at 16:10
  • I've just tried on my ubuntu and I most definitely cannot. You are not reading from /proc/self/mem are you? – Philip Couling Apr 10 at 16:10
  • foo(){ echo foo; }; export -f foo; bash -c 'foo' – pizdelect Apr 10 at 16:11
  • Nobody forces you to "waste time" reading my comments. But FWIW, the answer to that question is outdated -- you no longer have to attach with ptrace to a process in order to read or write to /proc/[pid]/mem. That's why you should always test your stuff and consult source code and primary docs instead of random stuff from internet forums ;-) – pizdelect Apr 10 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.