find the script's own folder
Inside a script, find the folder the script is in, to reach files next to it.
Steps
-
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"the script's folder, as a full path
-
cd "$dir"work from there
-
echo "$(basename "$0")"the script's own name
-
realpath "$0"its full path, links resolved
Commands used
dirnamerealpath
Each has a page in lore: lore dirname opens it.
Related recipes
- ask for input in a scriptAsk yes or no, read a password without showing it, or wait for an answer with a time limit.
- check if a file or folder existsTest in a script whether a path exists, is a file, a folder, empty or runnable.
- check if a string contains a textTest in a script whether a string contains, starts with or matches something.
- get a file's name, folder and extensionTake a path apart in a script: the folder, the name, the name without its extension, the extension.