help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why does </proc/self/environ not work in bash?


From: Christoph Anton Mitterer
Subject: Re: why does </proc/self/environ not work in bash?
Date: Mon, 12 Feb 2024 00:07:06 +0100
User-agent: Evolution 3.50.3-1

On Sun, 2024-02-11 at 09:16 -0500, Greg Wooledge wrote:
> I'm not sure what "/proc/self" actually is.

I think it always points to the PID of whoever open it.
https://github.com/torvalds/linux/blob/841c35169323cd833294798e58b9bf63fa4fa1de/Documentation/filesystems/proc.rst#L127-L128


> 
> unicorn:~$ ls -l /proc/self
> lrwxrwxrwx 1 root root 0 Feb 10 04:12 /proc/self -> 20906/
> unicorn:~$ ls -l /proc/self
> lrwxrwxrwx 1 root root 0 Feb 10 04:12 /proc/self -> 20907/
> 
> In this case, it seems to be using the PID of "ls" rather than the
> PID
> of the shell.

Yes, because it's ls which gets /proc/self as a plain argument and ls
which does any resolvin.


> If you wanted the shell's environment variables, you'd be better off
> using $$ instead:

Sure, but see my other reply from just a few minutes ago... for my
actual use case that's not what I want (i.e. I want the PID of the
process).

My finding about the differing behaviour when redirecting a /proc/self
file were just an accident and won't be a problem for my actual use.



> Bash forks a subshell to run tr with the redirection being performed
> inside the subshell.

Fair enough, and that - I guess - is what will happen when I do it as
part of a pipe.
But then I don't understand why comm has the name of the process
(shouldn't it be at worst something like bash-subshell or so ^^?) and
even less why the environment seems empty (shouldn't it be the same
than the shell's?).


Thanks,
Chris.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]