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: Kerin Millar
Subject: Re: why does </proc/self/environ not work in bash?
Date: Sun, 11 Feb 2024 11:04:48 +0000

On Sun, 11 Feb 2024 05:00:16 +0100
Christoph Anton Mitterer <calestyo@scientia.org> wrote:

> Hey there.
> 
> I stumbled over something which I cannot really explain:
> 
> Doing e.g.:
>   $ tr '\000' '\n' </proc/self/environ
> in bash gives no output, while e.g. in dash
> it gives the env vars, as I'd have expected it.
> 
> First question here is: whose environment should/would it return? The
> shell's? tr's?
> I'd assume the former, because the shell sets up the redirection
> 
> 
> But it gets even weirder when trying e.g. /proc/self/comm:
>   $ tr '\000' '\n' </proc/self/comm 
> in dash, gives
>   dash
> as I'd have expected it.
> 
> But in bash it gives:
>   tr
> 
> 
> Which would be what I'd expect if one did e.g.
>   $ cat /proc/self/comm
> where the program get's the path as argument and opens it, but not
> when doing:
>   $ cat </proc/self/comm
> 
> 
> Any ideas?

Interestingly, your command may also fail to behave as you expected in dash, 
albeit under specific circumstances.

$ dash -c 'tr "\0" "\n" < /proc/self/environ' | wc -c
0
$ dash -c 'tr "\0" "\n" < /proc/self/environ; true' | wc -c
2476

In the first case, dash refrains from forking for tr(1) on the basis that it be 
the final command, instead choosing to replace itself. This only seems to be 
the case for -c; I was unable to reproduce this behaviour in an executable dash 
script. It can lead to other surprising behaviours.

$ env -i invalid-to-dash=1 dash -c 'cat /proc/$$/environ'; echo
PWD=/home/kerin
$ env -i invalid-to-dash=1 dash -c 'cat /proc/$$/environ; true'; echo
invalid-to-dash=1

I don't know whether bash is in the wrong per se, but would suggest that you 
open /proc/$$/environ instead.

-- 
Kerin Millar



reply via email to

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