I got nerd-sniped by this. I can't see any reason why this wouldn't work. I tested this myself by writing a script called show-error.sh:
#!/bin/bash
echo $?
And then I ran $(exit 5)
. ./show-error.sh
And this outputs 5I got nerd-sniped by this. I can't see any reason why this wouldn't work. I tested this myself by writing a script called show-error.sh:
#!/bin/bash
echo $?
And then I ran $(exit 5)
. ./show-error.sh
And this outputs 5If you just run it it won't work.
So instead of running `boop` you would always have to `source boop` or `. boop`. A function or alias you could just run like normal.
(and `export ?` complains about ? being an invalid identifier in bash, in zsh `export "?"` apparently "works", but will reset it before you get a chance to print it in another process)
$(exit 5) ./show_error.sh
outputs 0.
Sourcing the file is the same as copying and pasting its content into the current shell. It is designed to allow this kind of things (accessing the caller execution context). But it is not what we usually call a script precisely because of this.
$(exit 5)
just spawn a subshell which exits with 5 which is visible by the parent script you just source your show-error.sh in?
Try to subshell the source too like
(. ./show-error.sh)