Skip to content

Redirect stderr from inner functions using command substitutions ("subshells") #1035

Open

Description

@terlar

It does not seem to be possible to redirect stderror from inner functions using "subshells".

function example_error
  if not contains '-a' b c
    echo 'Not contains'
  else
    echo 'Contains'
  end
end

function example_runner
  set -l result (example_error)
  echo $result
end

example_runner ^/dev/null

This will give:

contains: Unknown option “b”
/home/terje/test.fish (line 2): if not contains '-a' b c
                                ^
in function “example_error”,
    called on line 10 of file “/home/terje/test.fish”,

in command substitution
    called on line 10 of file “/home/terje/test.fish”,

in function “example_runner”,
    called on line 14 of file “/home/terje/test.fish”,

Not contains

There are two different ways to solve this:
Either do the redirection within the subshell (example_error ^/dev/null) or wrap everything inside a fish call fish -c 'example_runner' ^/dev/null. Neither solution is very good.

Activity

  1. added
    bug
    and removed on Jul 5, 2017
  2. faho commented on Jul 5, 2017

    @faho
    Member

    I copied @ridiculousfish's assignment from #2927. Hope that's okay!

  3. krader1961 commented on Jul 18, 2017

    @krader1961
    Contributor

    From my opening problem description in #4224:

    While working on the fix for issue #4211 I noticed that the exec_subshell() function only collects the stdout of the sub shell. This means that any output written to stderr by fish script run via that function is written to file descriptor 2 and cannot be redirected (e.g., to /dev/null) or otherwise processed by the code that calls exec_subshell(). This is broken. It's also an example of how the current fish design for managing file descriptors and stdin/stdout/stderr is problematic.I'm going to work around this by documenting that argparse validation code write error messages to stdout rather than the more natural stderr. But having to do so is quite annoying.

  4. changed the title [-]Redirect stderr from inner functions using "subshells"[/-] [+]Redirect stderr from inner functions using command substitutions ("subshells")[/+] on Aug 11, 2017
  5. IsaacOscar commented on Oct 26, 2023

    @IsaacOscar
    Contributor

    I'm not sure if this is the same bug or a different one,
    But I wanted to do something like this

    echo foo >/nonexistent/path 2>/dev/null
    

    Where any write error for the redirect get's silenced.
    Obviously that doesn't work as the error isn't produced by echo but fish itself.
    However, I thought these would work:

    begin; echo foo >/nonexistent/path; end 2>/dev/null
    eval "echo foo >/nonexistent/path" 2>/dev/null
    

    Which they didn't, and using subshells or functions like the start of this issue suggested doesn't work either. Using eval also doesn't work.
    The only solution I could get working is to use fish -c but I'd rather nat (as I have to then ensure everything I pass to it is properly escaped, and will likely be way slower).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions