Skip to content

Instantly share code, notes, and snippets.

@gwpantazes
Created October 18, 2017 22:52
Show Gist options
  • Save gwpantazes/d71bf815f20ca7605284f53b5378f90a to your computer and use it in GitHub Desktop.
Save gwpantazes/d71bf815f20ca7605284f53b5378f90a to your computer and use it in GitHub Desktop.
Running scripts from other directory
echo "a.sh is run"
pwd
echo ""
echo "First B"
# Doesn't work if in a different directory
source b.sh
echo ""
echo "Second B"
# DOES work from different directory
source $(dirname "$0")/b.sh
echo "b.sh is run"
pwd
echo "Script location: $0"
user:~$ echo $HOME
/Users/user
user:~$ ls
test/
user:~$ cd test
user:~/test$ ./a.sh
a.sh is run
/Users/user/test
First B
b.sh is run
/Users/user/test
Script location: ./a.sh
Second B
b.sh is run
/Users/user/test
Script location: ./a.sh
user:~/test$ cd ~
user:~$ test/a.sh
a.sh is run
/Users/user
First B
test/a.sh: line 7: b.sh: No such file or directory
Second B
b.sh is run
/Users/user
Script location: test/a.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment