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
{{ message }}
Repository navigation
Running intermediate try scripts with the original shell #7
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.
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:
The $SHELL variable will contain the name of the login shell (it doesn't work if the subprocess is different than the login shell)
Using ps, e.g., ps -o args= -p "$$" or ps -o command= -p "$$". This seems to me like the most portable and correct solution.
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.
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.
For Linux I recommend using stat --format='%N' /proc/$$/exe, which will output something like '/proc/3339774/exe' -> '/bin/bash'
For FreeBSD I recommend using procstat -bh $$, which will output something like 78297 sh 1400073 /bin/sh (we need to use all fields after the third to take into account spaces in paths)
On macOS consider using lsof -p $$ -Fnf | sed -n '/^ftxt/{n;/dlyd/d;s/^n//;p;}' (The sed command prints the output of txt entries, removing the dynamic loader entry, which I've seen appear in older macOS versions).
If the $TRY_SHELL variable is specified and executable, use that. (E.g., TRY_SHELL=/usr/bin/bash5.3 try ...).
If $SHELL is specified and executable, use that.
Try to find the user's login shell in /etc/passwd. If it's an executable program, use that.
If none of the above hold---$TRY_SHELL and $SHELL are unset and the login shell isn't executable---just use /bin/sh.
If the parent process of the try /bin/sh process is a particular shell, we should use that. (Look in /proc/$PPID/exe.) How would we know if it was a shell or not?
Activity
angelhof commentedon May 24, 2023
@mgree and @dspinellis any thoughts? I can implement the
pssolution, I just need a second opinion on whether it is adequately portable :)dspinellis commentedon May 25, 2023
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 scriptI will getshwithout 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.stat --format='%N' /proc/$$/exe, which will output something like'/proc/3339774/exe' -> '/bin/bash'procstat -bh $$, which will output something like78297 sh 1400073 /bin/sh(we need to use all fields after the third to take into account spaces in paths)lsof -p $$ -Fnf | sed -n '/^ftxt/{n;/dlyd/d;s/^n//;p;}'(The sed command prints the output oftxtentries, removing the dynamic loader entry, which I've seen appear in older macOS versions).mgree commentedon May 25, 2023
I agree that
psdoesn'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 commentedon Nov 17, 2024
@mgree which APIs do you speak of? Are you referring to the APIs exposed by
tryor APIs to detect the shell path?mgree commentedon Nov 19, 2024
I'm talking about
unshare.mgree commentedon Jun 2, 2025
I propose a three-tiered approach:
$TRY_SHELLvariable is specified and executable, use that. (E.g.,TRY_SHELL=/usr/bin/bash5.3 try ...).$SHELLis specified and executable, use that./etc/passwd. If it's an executable program, use that.$TRY_SHELLand$SHELLare unset and the login shell isn't executable---just use/bin/sh.If the parent process of the tryHow would we know if it was a shell or not?/bin/shprocess is a particular shell, we should use that. (Look in/proc/$PPID/exe.)Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
28 remaining items
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Added setting try shell and check_shell test. Fixes (binpash#7)
Run try command in the users shell of choice (#197)
mgree commentedon Jun 18, 2025
Closed by #197