Skip to content

Remove caret (^) as redirector for stderr #4394

Closed

Description

@ThomasAH

I propose removing the caret (^) as a redirector for stderr in fish 3.0.

Reason 1: This is surprising for users of other shells and may lead to data loss

There are many commands using the caret on the command line for special purposes, e.g. git (see #1873). While some special handling has been introduced to reduce the need for quoting (see Reason 2), this does not help in all cases, e.g. git log ^refA refB will create a file refA containing the error message of git.
Many other programs use ^, too, for me the most notable problem is grep.
I often use the command dpkg -l|grep -v ^ii to check for problems with not fully installed or purged Debian packages. In fish this prints nothing, and grep's error message is sent to the new file ii.
Or I use ps auxww|grep ^theuser to quickly find processes of some users.
Or grep ^http someconfigfile to see if the config file contains a setting about httpserver or httpproxy or ...

While this often just results in a stray file (which is already bad enough), this can easily overwrite existing files, because sometimes I am looking for something where a file of the same name exists, e.g. Makefile targets (which luckily can often be rebuilt), or filtered output of ls, e.g. ls | grep -v ^README to show me everything but README (which is now overwritten) and various README.foo files. Or ls | grep -v ^access.log | grep -v ^error.log to hide the current and rotated web server log files and see what other files are present. Yes, I could add quotes and I should add a backslash in front of the dot in the regexp, but I really don't expect a file accessblog here, so I don't do it in interactive use.

Reason 2: Workarounds have been introduced, but they introduce even more surprising behavior.

To workaround the problem in git (#1873), the handling of ^ was changed to be different than the handling of >, so echo foo^bar prints foo^bar, but echo foo ^ bar prints foo and creates an empty file bar. This is different from e.g. echo foo>bar and echo foo > bar which both do the same thing.

The need for this workaround indicates that using the caret as a redirection character is problematic.

Reason 3: The law of orthogonality (https://fishshell.com/docs/current/design.html#ortho)

^ is just a shortcut for 2>, so this syntax saves only a single character.
The user still has to know the difference between stdout and stderr to use this feature.
The user still has to know about file descriptors to redirect both, stdout and stderr, to the same file with echo Hello > all_output.txt ^&1.

Activity

  1. mqudsi commented on Sep 10, 2017

    @mqudsi
    Contributor

    I would be on board with this. It's just not a feature worth having.

  2. ridiculousfish commented on Sep 11, 2017

    @ridiculousfish
    Member

    I like this too.

  3. added this to the milestone on Sep 11, 2017
  4. zx8 commented on Sep 13, 2017

    @zx8

    Wow, I thought I was the only one who considered this a misfeature!

    I'm very much in favour of its removal as well.

  5. krader1961 commented on Sep 14, 2017

    @krader1961
    Contributor

    FWIW, I agree this "feature" should be removed. But note that it doesn't affect just ^/a/path to redirect stderr to /a/path. It also affects ^| a_command. We have two such uses of the latter in the fish project: share/completions/apm.fish and share/functions/__fish_print_make_targets.fish. That there are only two such instances in the fish code suggests that use case is relatively rare. Nonetheless it is another use of the feature which will be broken if we implement this change.

  6. ThomasAH commented on Sep 14, 2017

    @ThomasAH
    Author

    ^| is nice for some situations, but there are already some alternative ways to do this:

    if apm -h ^| string match -q "*Atom Package Manager*"
    can be changed to
    if apm -h 2>&1 | string match -q "*Atom Package Manager*"

    make -C $directory -d g1 -rn >/dev/null ^| awk ...
    can be changed to
    make -C $directory -d g1 -rn 2>&1 >/dev/null | awk ...

    If some really needs to print stdout and just wants to pipe/filter stderr, both can be swapped, e.g.:

    ls -ld /etc /missing /invalid 3>&2 2>&1 1>&3 | grep miss
    drwxr-xr-x 196 root root 12288 Sep 12 15:54 /etc
    ls: cannot access /missing: No such file or directory
    

    If above syntax is considered too complicated, the behaviour of | could be adjusted to match the behaviour of > by allowing file descriptors, so 2| would pipe stderr. This is more consistent, allows piping of other file descriptors, and accidents are far less likely to cause data loss, because e.g. grep foo 2|grep bar will not clobber existing files.

  7. mqudsi commented on Sep 14, 2017

    @mqudsi
    Contributor

    There's already a good workaround for it, one that is even less likely to be triggered by accident.

    mqudsi@ZBook /m/c/U/Mahmoud> fish -c "echo hello >&2" | grep foo
    hello
    mqudsi@ZBook /m/c/U/Mahmoud> fish -c "echo hello >&2" 2>| grep foo
    mqudsi@ZBook /m/c/U/Mahmoud> fish -c "echo hello >&2" 2>| grep hello
    hello

    I think it's a highly underrated feature, and I don't think any other "modern" shell makes it this easy.

  8. changed the title [-]RFC: Remove caret (^) as redirector for stderr[/-] [+]Remove caret (^) as redirector for stderr[/+] on Oct 10, 2017
  9. scooter-dangle commented on Feb 15, 2018

    @scooter-dangle

    What's fish's story on deprecation warnings? I pretty liberally use ^/dev/null when I intend to ignore errors. (Although it will be pretty annoying to get a deprecation notice on stderr if I'm already explicitly ignoring errors for a particular command/function.)

  10. ridiculousfish commented on Apr 1, 2018

    @ridiculousfish
    Member

    The 3.0 release is making these sort of changes without a deprecation phase, in part for the reason you say

  11. added a commit that references this issue on Apr 1, 2018
    e653fea
  12. lilyball commented on Apr 9, 2018

    @lilyball
    Contributor

    No deprecation phase is problematic because it means existing scripts will simply break without warning. This apparently includes fisherman and at least one fisherman plugin.

    Could we at least add a deprecation phase for master which we then turn off when releasing 3.0? That way people testing on master can try and at least flag shared components (like fisherman) that need to be updated.

  13. 8 remaining items

  14. jorgebucaran commented on Sep 18, 2018

    @jorgebucaran

    👏 Good job everyone and thank you, @ThomasAH for getting the ball rolling on this.

  15. added a commit that references this issue on Dec 28, 2018
  16. adeftu commented on Jan 4, 2019

    @adeftu

    Issue is marked with fish 3.0.0 milestone and closed. However current 3.0.0 version does not include it.

  17. floam commented on Jan 4, 2019

    @floam
    Member

    It's hidden behind a feature flag, stderr-nocaret.

  18. added a commit that references this issue on Jun 13, 2019
  19. ron-wolf commented on Nov 12, 2019

    @ron-wolf

    @floam Doesn’t that kind of defeat the purpose? Either run into weird behavior with ^—which is un-fish-like for the reasons @ThomasAH mentioned above—or add an extra line to your config files, to handle your otherwise smooth shell’s one weird behavior.

  20. faho commented on Nov 12, 2019

    @faho
    Member

    @ron-wolf The feature flag is purely a transition thing, and will be removed in a future version of fish.

  21. added a commit that references this issue on Mar 1, 2020
  22. locked as resolved and limited conversation to collaborators on Apr 16, 2020
  23. faho commented on Jul 27, 2022

    @faho
    Member

    For posterity: All code for the ^ redirection has been removed in fish 3.5.0, after it was disabled by default in fish 3.3.0.

    The feature flag remains available to check, but it will always return true. See #8857 for more information.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions