How to pass a variable to an SH child process?
– Unix & Linux Stack Exchange Closed 8 years ago. How to “send” variable to sub-shell? I have a bash script and inside that script I do sh. How to pass a variable to the new shell?
Is the subshell a child process in Bash?
In bash a subshell is a child process, other shells differ but even then a variable setting in a subshell does not affect the caller.
Is it safe to print an environment variable in Bash?
Given a POSIX sh, that should be plenty safe without any risk of arbitrary code execution, while still printing any exported environment variables (of the kind a shell can understand) to standard-out. Not the answer you’re looking for? Browse other questions tagged bash shell environment-variables or ask your own question.
How to change the variable from the parent shell?
Instead of accessing the variable from the parent shell, change the order of the commands and use the process substitution: Alternatively, when job control is inactive (e.g. in scripts) you can use the lastpipe shell option to achieve the same result without changing the order of the commands:
How are environment variables passed to a process?
The above assumes that the process is POSIX compliant, meaning that the process manages its environment using a global variable char **__environ as specified in the Ref Spec. The initial environment for a process is passed to the process in a fixed-length buffer on the process’s stack.
Is it possible to pass variables from child to parent?
You can transmit variables values from a child to its parent process through a file or a named pipe. Here is a theoretical simplest example: This is very tricky if the parent process is not expecting and cooperating with it.
Why do programs not change their own environment?
But many programs don’t bother changing their own environment, because it’s a bit pointless: a program’s environment is not visible through normal channels, only through /proc and ps, and even not every unix variant has this kind of feature, so applications don’t rely on it.