binpash / try

Inspect a command's effects before modifying your live system
MIT License
5.21k stars 68 forks source link

Running intermediate try scripts with the original shell #7

Open angelhof opened 1 year ago

angelhof commented 1 year ago

try currently runs the intermediate temporary scripts that it creates using /bin/sh. This causes a problem if we want the internal script to run using a different shell and therefore inherit its state, e.g., bash functions.

A running example of the wanted behavior follows:

$ func() { echo hi; }
$ export -f func
$ bash ./deps/try/try func
hi

I think that a good portable solution would be to run the intermediate scripts with whatever shell try is running too. What is a portable way to determine which shell we are running on? Here are some alternatives:

Here are some SO posts that discuss this issue:

Note that we don't really care about the underlying shell, we just want the executable (or even a link to it), so if try was invoked using /bin/sh we can just use that, we don't care that it was bash under the hood.

angelhof commented 1 year ago

@mgree and @dspinellis any thoughts? I can implement the ps solution, I just need a second opinion on whether it is adequately portable :)

dspinellis commented 1 year ago

I don't think the ps way is accurate enough, because it gives the name of the command that invoked the shell, but not the actual shell path. For example, if I execute a script with sh script I will get sh without a path, and won't know whether this is a link to, say, bash or sh. Here are the ways I recommend for commonly used systems.

mgree commented 1 year ago

I agree that ps doesn't quite cut it. These detection approaches seem good; we should also have a flag that lets you pick the shell that runs.

Also: these APIs only exist on Linux and FreeBSD, so we don't need to worry about macOS portability.

kwakubiney commented 6 days ago

@mgree which APIs do you speak of? Are you referring to the APIs exposed by try or APIs to detect the shell path?

mgree commented 3 days ago

I'm talking about unshare.