Skip to content

Instantly share code, notes, and snippets.

@antonfisher
Last active December 4, 2015 10:07
Show Gist options
  • Save antonfisher/fb8a9bdb4b9fc2d44134 to your computer and use it in GitHub Desktop.
Save antonfisher/fb8a9bdb4b9fc2d44134 to your computer and use it in GitHub Desktop.
BASH: get_full_file_path
##
# Get full file/directory path
#
# Examples:
# full_path=$(get_full_path '/tmp'); # /tmp
# full_path=$(get_full_path '~/..'); # /home
# full_path=$(get_full_path '../../../'); # /home
#
# @param {String} $1 - relative path
# @returns {String} absolute path
#
function get_full_path {
local user_home;
local user_home_sed;
local rel_path;
local result;
user_home="${HOME//\//\\\/}";
user_home_sed="s#~#${user_home}#g";
rel_path=$( echo "${1}" | sed "${user_home_sed}" );
result=$( readlink -e "${rel_path}" );
echo "${result}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment