Replies: 2 comments 3 replies
-
If you need this for scripting you can use from contextlib import contextmanager
@contextmanager
def chdir(adir):
old_dir = os.getcwd()
os.chdir(adir)
yield
os.chdir(old_dir)
# --------------------------------------------
cd /tmp
mkdir -p dir1
pwd
with chdir("./dir1"):
pwd
pwd
# /tmp
# /tmp/dir1
# /tmp Running commands using the subshell syntax is the new feature for xonsh. |
Beta Was this translation helpful? Give feedback.
-
hi! I'm going to answer generally because I feel the pressure of posix shells users expectations. The xonsh shell is young and developed by the volunteers without strong release cycles. Current behavior of parser, threading and the logic of how it's working together were constructed for cover pretty minimal needs. So we can't say that the logic is weird we can say that the logic is on the beginner level. To push the improvement forward:
(I see that my messages has "Maintainer" label but the "maintainer" is the team during a couple of years. Don't treat my comment as a comment from owner.) |
Beta Was this translation helpful? Give feedback.
-
Basically, this is what I want to run (example for zsh and bash):
Basically,
cmd1
andcmd2
should run indir1
andcmd2
should run indir2
.In
bash
, the () syntax creates a subshell which is basically just bash running the subshell commands in a fork; inzsh
, IIUC, it simulates this mechanism, managing the directory stack and env variables, without actually forking.Is there a way to do something similar in xonsh?
What I have tried, this doesn't create a subshell, the
cd
effect isn't reversed when exiting the parentheses:This and others like it
$()
,!()
,$[]
,![]
, are syntax errors:Closest I could get is this, but this is clunky as the subshell commands doesn't get syntax highlighted, and it'll probably fail on more complex scenarios with variable substitutions and nested subshells:
One workaround is doing this with a macro, this works ok if all you care about is isolating the cwd changes:
but it still doesn't handle variable scopes as expected:
Beta Was this translation helpful? Give feedback.
All reactions