Skip to content

Provide subshell like functionality #1439

Open

Description

@jorbsd

While fish avoids forking/execing, having subshell like functionality would still be very nice.

In bash/zsh:

pwd; (cd ; ; pwd;); pwd

should be in the first dir, change and do stuff, then show that you are back in the original did

This closure of shell state in the subshell (vars, directories, etc.) is very handy.

bash/zsh modern syntax uses $() to be similar to () in fish, both replacing ``

Activity

  1. Debilski commented on May 15, 2014

    @Debilski

    I think the question is whether this is useful for anything other than cd dir; …. You might get similar behaviour with pushd, popd and set -l in begin…end blocks and still retain the chance to escape the inner scope.

  2. sugar700 commented on May 15, 2014

    @sugar700
    Contributor
    fish -c '
        echo totally a subshell
        echo no, seriously
        cd ..
    '
  3. Debilski commented on May 16, 2014

    @Debilski

    @xfix: No colour highlighting though. ;)

  4. jorbsd commented on Jun 14, 2014

    @jorbsd
    Author

    ridiculoufish should have most of my thoughts on this from the last year when I used fish on and off and kept only going back to modified zsh to get subshells back each time. I wish I had kept better notes each time, but at least I always let him know what I was up to and my thoughts on subshells at the time

  5. cben commented on Aug 12, 2014

    @cben
    Contributor

    pushd/popd are inferior because an error (or manual Ctrl+C) leaves you in the wrong dir.
    A withd DIR COMMAND (not the best name) that reliably chdirs back would be very convenient
    (in fact, given fish's Alt+Left/Alt+Right navigation, I don't see why we need pushd/popd/dirs at all. Especially in addition to dirh/prevd/nextd).
    But then it can be useful with multiple commands, so it'd need to be a

    withd DIR
        COMMAND
        ...
    end
    

    builtin, at which point the specialization to changing directories is silly, it should really be

    subshell
        cd DIR
        ...
    end
    

    => How about just adding cd -l that works in blocks?

    Other use cases for subshells:

    • Temporary setting env vars. For one command env FOO=BAR COMMAND is very convenient;
      for multiple begin; set -l -x FOO BAR; COMMAND; ...; end works but is a bit awkward to remember (specifically -x is easy to forget - usually set does the right thing without it, but not set -l).
    • umask/ulimit. Very rare.
  6. ridiculousfish commented on Aug 12, 2014

    @ridiculousfish
    Member

    cd -l seems totally brilliant to me.

  7. jorbsd commented on Aug 12, 2014

    @jorbsd
    Author

    I thought "subshell begin; ...; end;" was pretty damn good.

    Maybe both for different uses?

    Sent from my iPhone

    On Aug 12, 2014, at 09:56, ridiculousfish [email protected] wrote:

    cd -l seems totally brilliant to me.

    —
    Reply to this email directly or view it on GitHub.

  8. added this to the fish-future milestone on Oct 8, 2014
  9. c22 commented on Aug 2, 2016

    @c22

    If I'm not mistaken, this problem kinda goes away if #563 is fixed, right?

  10. ridiculousfish commented on Aug 2, 2016

    @ridiculousfish
    Member

    Maybe, maybe not, depending on how it's fixed. A subshell is both concurrent execution and independent state (multiple $PWDs, etc).

  11. d commented on Aug 21, 2016

    @d

    variables (exported or not) is a case where I'd love to have a subshell:

    Scenario

    begin --subshell
        docker-machine env aws-fast | source
        docker run --rm --volume /ccache ubuntu tar c /ccache
    end | begin --subshell
        docker-machine env aws-slow | source
        docker run --rm --volume /ccache ubuntu tar x
    end
    

    Should be equivalent to the following Bash snippet:

    (
    eval "$(docker-machine env aws-fast)"
    docker run --rm --volume /ccache ubuntu tar c /ccache
    ) | (
    eval "$(docker-machine env aws)"
    docker run --rm --volume /ccache ubuntu tar x
    )
    
  12. ridiculousfish commented on Aug 27, 2016

    @ridiculousfish
    Member

    The begin --subshell syntax is pretty clever

  13. 49 remaining items

  14. added 11 commits that reference this issue on Apr 30, 2024
    3e9c2df
    4b82107
    5e5cbf3
    8f09b96
    0828590
    cda1885
    f7e8af8
    473e622
    9832bb3
    4b856b4
    a1ed94f
  15. akhilman commented on Jan 13, 2025

    @akhilman
  16. ZuBB commented on Feb 13, 2025

    @ZuBB

    In the online manual, there is a section about subject functionality. But it did not work for me. Also, this issue opened. Do we have outdated/wrong docs, or did I misread something?

  17. tmccombs commented on Feb 13, 2025

    @tmccombs
    Contributor

    That section says that fish doesn't have subshells, and lists some alternatives that work in some cases.

  18. stephane-chazelas commented on Oct 24, 2026

    @stephane-chazelas

    7. cd -l seems totally brilliant to me.

    You'd also want cdh -l, umask -l, function -l, trap -l (there's already block -l), alias -l, ulimit --local (ulimit -l already taken for something else).

    And decide what to do with pushd/popd and the directory stack and history.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions