You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 ``
Reacted by Beni Cherniavsky-Paskin, Daphne Preston-Kendal, Aleksei Zabrodskii, xster, Niklas Higi, Stas Shakirov, Nick Sweeting, Konrad Gądek, Felipe Borges, Cormac Relf and 68 more
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.
fish -c ' echo totally a subshell echo no, seriously cd ..'
Reacted by Nikita Bobko
Reacted by Beni Cherniavsky-Paskin, xster, Murad, Paulo Koch, Thai Pangsakulyanont, krnl0138, Immánuel!, Different55, Leonhardt Wille, Bouke van der Bijl and 15 more
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
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.
Reacted by kyb, Kevin M Granger, Billie Thompson, Mikel Ward, wallabra, Brendan Zabarauskas, Shea690901, Meghdut Mandal, Thayne McCombs, Olivier 'reivilibre' and 1 more
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
)
Reacted by David Adam, Aaron Gyes, Alex Wendland, Toli Paine, Beni Cherniavsky-Paskin, Nick Sweeting, Ed Brannin, Natalie Klestrup Röijezon, kyb, Murad and 10 more
Reacted by Marcus Borkenhagen, Jesse Zhang, Toli Paine, Nick Sweeting, Aleksei Zabrodskii, Roland Tepp, Murad, Piotr Kowalczuk, Leonhardt Wille, Mikel Ward and 9 more
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?
Activity
Debilski commentedon May 15, 2014
I think the question is whether this is useful for anything other than
cd dir; …. You might get similar behaviour withpushd,popdandset -linbegin…endblocks and still retain the chance to escape the inner scope.sugar700 commentedon May 15, 2014
Debilski commentedon May 16, 2014
@xfix: No colour highlighting though. ;)
jorbsd commentedon Jun 14, 2014
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
cben commentedon Aug 12, 2014
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
builtin, at which point the specialization to changing directories is silly, it should really be
=> How about just adding
cd -lthat works in blocks?Other use cases for subshells:
env FOO=BAR COMMANDis very convenient;for multiple
begin; set -l -x FOO BAR; COMMAND; ...; endworks 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).ridiculousfish commentedon Aug 12, 2014
cd -lseems totally brilliant to me.jorbsd commentedon Aug 12, 2014
I thought "subshell begin; ...; end;" was pretty damn good.
Maybe both for different uses?
Sent from my iPhone
c22 commentedon Aug 2, 2016
If I'm not mistaken, this problem kinda goes away if #563 is fixed, right?
ridiculousfish commentedon Aug 2, 2016
Maybe, maybe not, depending on how it's fixed. A subshell is both concurrent execution and independent state (multiple $PWDs, etc).
d commentedon Aug 21, 2016
variables (exported or not) is a case where I'd love to have a subshell:
Scenario
Should be equivalent to the following Bash snippet:
ridiculousfish commentedon Aug 27, 2016
The
begin --subshellsyntax is pretty clever49 remaining items
subshell abbreviation
Experimental raw quoting syntax
Insert a subshell when typing ( at the start of a command
Raw quoting syntax for first-class subshells
When typing ( at the start of a command, open a new subshell
Raw quoting syntax for first-class subshells
When typing ( at the start of a command, open a new subshell
Raw quoting syntax for first-class subshells
When typing ( at the start of a command, open a new subshell
Raw quoting syntax for first-class subshells
When typing ( at the start of a command, open a new subshell
akhilman commentedon Jan 13, 2025
ZuBB commentedon Feb 13, 2025
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?
tmccombs commentedon Feb 13, 2025
That section says that fish doesn't have subshells, and lists some alternatives that work in some cases.
stephane-chazelas commentedon Oct 24, 2026
You'd also want
cdh -l,umask -l,function -l,trap -l(there's alreadyblock -l),alias -l,ulimit --local(ulimit -lalready taken for something else).And decide what to do with
pushd/popdand the directory stack and history.