Borrowing graciously from this StackOverflow answer, I want to find the directory a script is running in so I can load relative paths for it on login:
The script is pretty small right now:
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR=$( cd -P "$( dirname "$SOURCE" )" && pwd )
But when I log in or call source <this_script.sh>
, I get:
-bash: script.sh: line 4: syntax error: unexpected end of file
Couple questions:
- What is it hanging up on?
- What's the fix?
This is running:
-bash-3.2$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
Edit 1:
trying Gile's solution:
parent="$(dirname "$SOURCE")"
echo "PARENT IS $parent"
DIR="$(cd -P "$parent" && pwd)"
echo "DIR IS $DIR"
I get the following when trying to source
it or log in:
Last login: Mon Mar 12 08:07:13 2012 from ....
PARENT IS .
: No such file or directory
DIR IS
: command not found
: command not found
: command not found
: command not found
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
. Any reason why you don't use that? – user unknown Mar 8 '12 at 16:22x="my name is ryran"
and thena=$x
ora=${x/ryran/nick klauer}
ora=$(echo $x)
– ryran Mar 8 '12 at 18:48$PWD
? (not worth an 'answer') – laebshade Mar 10 '12 at 0:23