Search code examples
shellfish

How to get the path of a fish script within itself


In bash, we can get the path of a script with $0. It seems that we can do that in a fish script by $_ according to the official document.

How ever, if I try to write a fish script like this, and run it, it will print just a newline.

#!/usr/bin/fish
echo $_

While in a reactive fish, echo $_ will give echo literately and <command> $_ will pass "command" literately as <command>'s argument.

I am confused about the strange behavior of $_, and what it is used for.


Solution

  • $_ is a bit confusing, but it's not the "path to the fish script", it's the name of the current foreground job.

    To get the path to the script, use status --current-filename (which will be "standard input" if launched interactively).