Question

In a fish shell script, is it possible to obtain the path where the shell script lives? In bash, you can use the BASH_SOURCE variable to do this.

Was it helpful?

Solution

One way is to use the status command.

eg.

~/tmp> echo status -f > show_filename.fish
~/tmp> fish show_filename.fish
/home/foo/tmp/show_filename.fish

--

Ah, sorry - judging by your comment, I misread the question. I thought you meant the fully qualified path, instead of the script's directory.

You could just send the status result to dirname. It's not a built-in function of fish, like status is, but fish tends to outsource as much of this functionality as it can anyway (see "The law of minimalism", over in the fish design doc). I can't see anything better commands in the docs for this, so maybe this is as good as we can get for now?

Amending the example, change the contents of show_filename.fish to be:

dirname (status -f)

Now you should get:

~/tmp> fish show_filename.fish
/home/foo/tmp
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top