Bash source Command: $0 Can Not Determine Its Own Location

last updated in Categories

The source command read and execute commands from a file. My script source a script called But, script can not determine it’s own location using $0. By default, $0 gives parent script location. How do I find out a script that is sourced at the command line can determine it’s own location? My sample script:

# /var/www/html/java/tasks/ 
# I want /var/www/html/java/tasks in _currentdir 
echo "${_currentdir}"

When I run from



source /var/www/html/java/tasks/

The $_currentdir is set to be the directory where lives which is in /root/scripts/java/helper or . (current) directory. How do I fix this problem?