Created
October 2, 2016 17:15
-
-
Save adamjstewart/c7629ed2ad89ed25513a82d3cb5d774e to your computer and use it in GitHub Desktop.
Bash relative vs. absolute paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# There are many ways to get a path to a file or script. | |
# This script showcases several of them and their pitfalls. | |
# Credit for most of these techniques comes from: | |
# http://stackoverflow.com/questions/4774054/ | |
abs_path='/usr/bin/bash' | |
rel_path='.' | |
########################################################### | |
# Using dirname | |
########################################################### | |
# dirname works great when given an absolute path. | |
dirname "$abs_path" | |
# But only gives a relative path otherwise. | |
dirname "$rel_path" | |
########################################################### | |
# Using readlink -f | |
########################################################### | |
# readlink -f works great and even works for symlinks. | |
readlink -f "$abs_path" | |
readlink -f "$rel_path" | |
# Unfortunately, it only works on Linux, not on macOS. | |
########################################################### | |
# Using a combination of cd and pwd | |
########################################################### | |
# The most reliable way to get an absolute path is a | |
# combination of cd and pwd. Just make sure to do it | |
# in a subshell, lest you change your current directory. | |
(cd "$(dirname "$abs_path")" && pwd) | |
(cd "$(dirname "$rel_path")" && pwd) | |
########################################################### | |
# Resolving symlinks | |
########################################################### | |
# The cd && pwd method can be used to resolve symlinks | |
# by using the -P option to pwd. | |
(cd "$(dirname "$abs_path")" && pwd -P) | |
(cd "$(dirname "$rel_path")" && pwd -P) | |
########################################################### | |
# Getting the location of a script | |
########################################################### | |
# But what if you want to know the location of the script | |
# you are currently running? You have two options. | |
# $0 returns the name of the script you are running | |
# but returns -bash if you source the script. | |
echo "$0" | |
(cd "$(dirname -- "$0")" && pwd -P) | |
# ${BASH_SOURCE[0]} always returns the name of the script, | |
# even if you source the file. | |
echo "${BASH_SOURCE[0]}" | |
(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P) | |
# Note that I wrap all variables and the output of these | |
# variables in double quotes. This is intentional, as | |
# any spaces in the filenames or directories could cause | |
# unintended consequences. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks a million!