Reputation: 51435
So, I really like Fish - but I need some help with scripting. and in particular finding the path of the script being run.
Here is the solution for BASH Getting the source directory of a Bash script from within
Can anyone help me find the equivalent with fish?
Upvotes: 20
Views: 6550
Reputation: 378
dirname (status --current-file)
didn't work for me so I tried basename (pwd)
and it works
Upvotes: 0
Reputation: 11
Since readlink -m
does not exist on macOS,
DIR=dirname (realpath (status -f))
Works on macOS and Linux at the same time.
This solution also has the benefit to work from an .sh file or even the command line.
Upvotes: 1
Reputation:
In fish shell,
set DIR (cd (dirname (status -f)); and pwd)
is an equivalent to the BASH one liner
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
mentioned in Can a Bash script tell what directory it's stored in?.
NOTE: fish shell will cd
into that dir and stay there. Bash will cd
but it stays contained in subcommand.
Upvotes: 11
Reputation: 513
To get the full path of the script, use status --current-filename
, or
set FILE (status --current-filename)
to declare a variable.
To get the full path to the directory the script is stored in, use dirname (status --current-filename)
, or set DIR (dirname (status --current-filename))
to declare a variable.
The equivalent to this question is the following:
set DIR (dirname (status --current-filename))
is the equivalent
Upvotes: 5
Reputation: 18551
status --current-filename
will output the path to the currently executing script.
For more information on the status command, you can run man status
or see the documentation at http://fishshell.com/docs/current/commands.html#status
Upvotes: 28