Lorin Hochstein
Lorin Hochstein

Reputation: 59202

Retrieve the path of the current source file with fish

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.

Upvotes: 8

Views: 1804

Answers (1)

otherchirps
otherchirps

Reputation: 1846

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

Upvotes: 9

Related Questions