2

I'm trying to execute several commands in a subshell, e.g. (cd ~ && pwd) in bash. I can do home >>= cd in the current shell to combine some operations, but I can't figure out how I can do the same in a subshell.

I went through the documentation and couldn't find something that looked promising, however, I was able to do it in this way inshell "cd ~ && pwd" mempty.

Even though this works, it does not compose as nicely and when needing to execute a bunch of commands it gets ugly.

Does anyone know how to achieve the composition of several operations in a subshell with the same style as done in the current shell?

dimo414
  • 47,227
  • 18
  • 148
  • 244
Carlos Morera
  • 377
  • 4
  • 12
  • It seems like you want a function `subshell :: Shell a -> Shell a` which runs a `Shell` in a subshell; unfortunately this doesn't really make sense. When you run `(..)` in bash, what you have is an outer bash program starting an inner bash program, which executes the specified commands. But `x :: Shell X` is a haskell program, not a bash program; it doesn't make sense to ask bash to start a Haskell program in a subshell (unless, of course, you compile that program to an executable or or intepret the script directly - but this isn't the same as starting an arbitrary computation in a subshell) – user2407038 Feb 09 '18 at 17:41
  • 1
    Do you actually want a subshell in this specific case? In a traditional Unix shell you'd use one because that's basically the only way to implement scopes, but in Haskell/Turtle you have the faster and more fine grained `using` – that other guy Feb 09 '18 at 18:04
  • @thatotherguy you are right, ultimately what I was really trying to do is to execute a bunch of commands in an isolated scope, e.g. cding into a directory to run a command in there, without having to cd back in the outer scope. P.S. could you please send a link to the `using` you are referring to? I thought it was part of turtle but I can't find it. – Carlos Morera Feb 09 '18 at 19:41
  • I was just looking at the [docs for Turtle.Shell](https://hackage.haskell.org/package/turtle-1.5.2/docs/Turtle-Shell.html), and it lists `using` which seemed like Control.Exception `bracket` style resource management. In this case it could presumably `cd` back to the original directory when "released". – that other guy Feb 09 '18 at 19:56

0 Answers0