I have a bash script file, which is put under some directory added to $PATH so that I can call the script from any directory.
There is another text file under the same directory as the script. I wonder how to refer to the text file in the script?
For example, if the script is just to output the content of the text file, cat textfile won’t work, since when calling the script from a different directory, the text file is not found.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
These should work the same, as long as there are no symlinks (in the path expansion or the script itself):
-
MYDIR="$(dirname "$(realpath "$0")")" -
MYDIR="$(dirname "$(which "$0")")" -
A two step version of any of the above:
MYSELF="$(realpath "$0")"MYDIR="${MYSELF%/*}"
If there is a symlink on the way to your script, then which will provide an answer not including resolution of that link. If realpath is not installed by default on your system, you can find it here.
[EDIT]: As it seems that realpath has no advantage over readlink -f suggested by Caleb, it is probably better to use the latter. My timing tests indicate it is actually faster.
Method 2
My systems do not have realpath as suggested by rozcietrzewiacz.
You can accomplish this using the readlink command. The advantage of using this over parsing which or other solutions is that even if a part of the path or the filename executed was a symlink, you would be able to find the directory where the actual file was.
MYDIR="$(dirname "$(readlink -f "$0")")"
Your text file could then be read into a variable like this:
TEXTFILE="$(<$MYDIR/textfile)"
Method 3
$0 in the script will be the full path to the script, and dirname will take a full path and give you just the directory, so you can do this to cat textfile:
$ cat "$(dirname -- "$0")/textfile"
Method 4
You can put this at the top of your script:
cd "${BASH_SOURCE%/*}" || exit
The BASH_SOURCE internal bash variable is actually an array of
pathnames. If you expand it as a simple string, e.g. “$BASH_SOURCE”,
you’ll get the first element, which is the pathname of the currently
executing function or script.
Source: http://mywiki.wooledge.org/BashFAQ/028
Method 5
I was trying these and realpath didn’t work for me. The solution I went with is:
SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
Which has worked well so far. I would like to know if there are any potential issues with the approach.
Method 6
I use:
#! /bin/sh -
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P) || exit
dosomethingwith "${dir%/}/some-file"
Which is POSIX and should work as long as the dirname of $0 doesn’t end in newline characters, is not - and $CDPATH is not set (and possibly a few other corner cases if the script was not looked-up in $PATH).
Method 7
I always use which to find the full path of an executable from the PATH. For instance:
which python
If you combine this with the dirname command then you get:
wp=`which python` dn=`dirname $wp` ls $dn
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0