array

The source command read and execute commands from a file. My parent.sh script source a script called child.sh. But, child.sh 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:

#!/bin/bash
# /var/www/html/java/tasks/child.sh
_me="$0"
# I want /var/www/html/java/tasks in _currentdir
_currentdir="${_me%/*}"
echo "${_currentdir}"

When I run from parent.sh:

source child.sh

OR

source /var/www/html/java/tasks/child.sh

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

{ 1 comment }

How do I find out ram information under Linux operating systems?

{ 7 comments }

How do I check if a bash shell variable called $input is defined or not under BSD / Apple OS X / Unix / Linux like operating systems?

{ 3 comments }

How do I iterate through an array under Bash scripting?

{ 1 comment }

Q. How do I redirect with PHP script?

{ 58 comments }

Q. How do I use bash for loop to iterate thought array values under UNIX / Linux operating systems?

{ 7 comments }