jbang icon indicating copy to clipboard operation
jbang copied to clipboard

shebang that doesn't keep a shell around?

Open sixcorners opened this issue 1 month ago • 1 comments

Think it would be a good idea to change the shebang line described everywhere? ///usr/bin/env jbang "$0" "$@" ; exit $? This line keeps a bash shell around. Or a dash shell if you run this from zsh I guess. ///usr/bin/env true; exec jbang "$0" "$@" This doesn't. It runs jbang with exec so the shell process goes away. What do you all think?

///usr/bin/env true; exec jbang "$0" "$@"
///usr/bin/env jbang "$0" "$@" ; exit $?

static void printTree(ProcessHandle ph, int level) {
  System.out.printf("%s[%d] %s%n", "  ".repeat(level), ph.pid(), ph.info().command().orElse("?"));
  ph.parent().ifPresent(parent -> printTree(parent, level + 1));
}

void main() {
  printTree(ProcessHandle.current(), 0);
}
$ ./asdf.java 
[2951097] /snap/graalvm-jdk/26/graalvm-ce/bin/java
  [2951096] /usr/bin/bash
    [130156] /usr/bin/bash
      [971812] /usr/libexec/gnome-terminal-server
        [6655] ?
          [1] ?

vs

$ ./asdf.java 
[2951588] /snap/graalvm-jdk/26/graalvm-ce/bin/java
  [130156] /usr/bin/bash
    [971812] /usr/libexec/gnome-terminal-server
      [6655] ?
        [1] ?

Also with this new shebang line when you System.out.close() it will close the fd 1 associated with the process which works more like how java/jbang works without the shebang. With the old shebang line the shell will hold fd 1 open until the java process exits. Kind of a small difference.

sixcorners avatar Dec 08 '25 20:12 sixcorners

I'll need to check. I seem to recall the issue is that exit codes does not propagate correctly without the exit?

maxandersen avatar Dec 09 '25 18:12 maxandersen