0

Say I login using bash and I define an alias.

$ alias c=clear
$ echo $0
bash

Now I enter dash.

$ dash
$ echo $0
dash

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.

alias
  • 35

1 Answers1

1

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:

x=foo
export x

dash
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. –  Apr 10 '19 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. –  Apr 10 '19 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 '19 at 16:10
  • foo(){ echo foo; }; export -f foo; bash -c 'foo' –  Apr 10 '19 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 ;-) –  Apr 10 '19 at 16:31
  • @pizdelect Welcome to stack exchange, a series of Q&A sites which were designed to improve on an earlier model of forums. One of the main advantages over Forums is that forums require you to scroll through endless comments before you finally find your answer, where Q&A sites fix this by putting the answer first. Please don't use comments on Q&A sites to post answers because it breaks the whole point of the Q&A structure and wastes everyone's time by forcing them to scroll through endless comments. – Philip Couling Apr 11 '19 at 09:01