Adding >/dev/null 2>&1 at the end of your cd command will take care of both possibilities. This last one will work with any combination of aliases, source, bash -c, symlinks, etc.īeware: if you cd to a different directory before running this snippet, the result may be incorrect!Īlso, watch out for $CDPATH gotchas, and stderr output side effects if the user has smartly overridden cd to redirect output to stderr instead (including escape sequences, such as when calling update_terminal_cwd >&2 on Mac). ] & SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located While do # resolve $SOURCE until the file is no longer a symlinkĭIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 & pwd )" If you also want to resolve any links to the script itself, you need a multi-line solution: #!/usr/bin/env bash It will work as long as the last component of the path used to find the script is not a symlink (directory links are OK). Is a useful one-liner which will give you the full directory name of the script no matter where it is being called from.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |