coreutils
[Top][All Lists]
Advanced

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

Re: [PATCH v2] printenv,env: quote problematic variables


From: Collin Funk
Subject: Re: [PATCH v2] printenv,env: quote problematic variables
Date: Sat, 29 Aug 2026 12:56:24 -0700
User-agent: Gnus/5.13 (Gnus v5.13)

PƔdraig Brady <P@draigBrady.com> writes:

> On 06/08/2026 05:54, Collin Funk wrote:
>> PƔdraig Brady <P@draigBrady.com> writes:
>
>>> Yes, it makes sense to quote even when redirecting
>>> (unless QUOTE_STYLE=literal) as we were discussing at:
>>> https://github.com/coreutils/coreutils/issues/324
>>>
>>> One small worry is breaking usage like:
>>> foo=$(printenv foo) or popen("printenv foo").
>>> But that's not a practical concern as it's much more
>>> natural to access particular env vars directly.
>>> I did audit the following just in case:
>>> https://codesearch.debian.net/search?q=printenv.*\|.*(cut|sed)&literal=0
>>> but noticed no problematic usage.
>>> In fact quoting newlines in env var values would
>>> make the found usages more robust.
>> In the more general cases (ignoring many that are obvious tests), I
>> have
>> found it is mostly stuff like this:
>>     $ printenv LC_ALL
>>     [...]
>> That one should obviously not be defined in a way that needs quoted.
>> I have also seen stuff like this:
>>     $ printenv | grep '^SSH_'
>>     [...]
>> That one should probably not ever have the need to be quoted, but I
>> guess it is possible. In any case, that one would be buggy if someone
>> defines an environment variable with "SSH_" following a newline.
> Thinking a bit more about this, it might be safer to
> limit the single variable case to only quote to tty by default.
>
> For example to support usage like:
>
>   user_color=$(run0 --user="$user" printenv user_color)
>
> E.g. if $user_color contained ANSI escapes we'd want them unquoted.
> Note the trailing newline would be auto stripped in the above,
> allowing such usage.

I think I agree. It feels a little weird to behave differently based on
the number of arguments, but I think that case warrants it.

I've attached an incomplete patch to do that. I'll probably adjust NEWS
and the documentation a bit.

One thing I'm unsure about is whether a test for tty output should be
added to the same test or in misc/tty-quoting.sh. It seems like the ones
there are tailored to a single argument, where we should test both
single argument and multiple arguments in this case, I think. I don't
want that look to be *too* full of edge cases such that it gets
difficult to understand. WDYT?

Collin

Attachment: 0001-printenv-don-t-quote-output-with-no-terminal-and-a-s.patch
Description: Text Data


reply via email to

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