Quantcast
Channel: Get path of current script when executed through a symlink - Unix & Linux Stack Exchange
Browsing all 7 articles
Browse latest View live

Answer by MrYellow for Get path of current script when executed through a...

DIR=$( cd -- "$( dirname -- "$(readlink -f "${BASH_SOURCE[0]}" || ${BASH_SOURCE[0]})" )" &> /dev/null && pwd )BASH_SOURCE works with source ./script.sh where $0 does notreadlink supports...

View Article



Answer by blackjacx for Get path of current script when executed through a...

In my case (on macOS) I had to use:DIR=$(cd "$(dirname "$(readlink "$0" || echo "$0")")" && pwd)This is because the symlink or the script behind can both be called by users. So in both cases...

View Article

Answer by Kevin Campion for Get path of current script when executed through...

readlink -f doesn't work for me, I've this mistake:readlink: illegal option -- fusage: readlink [-n] [file ...]But this works fine:DIR="$(dirname "$(readlink "$0")")"echo $DIR

View Article

Answer by user2108420 for Get path of current script when executed through a...

One small addition to the above script. The -P option to pwd follows symlinksDIR="$(cd "$(dirname "$0")"&& pwd -P)"

View Article

Answer by diyism for Get path of current script when executed through a symlink

one line:cd $(dirname $([ -L $0 ] && readlink -f $0 || echo $0))

View Article


Answer by Caleb for Get path of current script when executed through a symlink

Try this as a general-purpose solution:DIR="$(cd "$(dirname "$0")" && pwd)"In the specific case of the following symlinks, you could also do this:DIR="$(dirname "$(readlink -f "$0")")"

View Article

Get path of current script when executed through a symlink

I have a utility consisting of a couple of directories with some bash scripts and supporting files that will be deployed to several machines, possibly in a different directory on each machine. The...

View Article
Browsing all 7 articles
Browse latest View live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>