linux - Environment variable with spaces in a string - How to use them from /proc/pid/environ -
i set variable spaces in string new bash:
var='my variable spaces' /bin/bash
and if want start new bash same environment, like:
env=$(cat /proc/self/environ | xargs -0 | grep =) env -i - $env /bin/bash
but thing is, in /proc/self/environ
, variable without quotes. last command throws a: env: variable: no such file or directory
how can work around limitation?
ps: simplified version of following issue: https://github.com/jpetazzo/nsenter/issues/62
i think answer here not use shell script set things up. using higher-level language makes easier parse /proc/<pid>/environ
useful. here's short example:
#!/usr/bin/python import os import sys import argparse def parse_args(): p = argparse.argumentparser() p.add_argument('pid') p.add_argument('command', nargs=argparse.remainder) return p.parse_args() def main(): args = parse_args() env = {} open('/proc/%s/environ' % args.pid) fd: envspec in fd.read().split('\000'): if not envspec: continue varname, varval = envspec.split('=', 1) env[varname] = varval print env os.execvpe(args.command[0], args.command, env) if __name__ == '__main__': main()
put in file called env-from
, make executable, , can run:
env-from <pid> bash
and you'll shell using environment variables specified process.
Comments
Post a Comment