Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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:

  1. What is it hanging up on?
  2. 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 ....
: No such file or directory
: command not found
: command not found
: command not found
: command not found
share|improve this question
The answer has been updated and is by now: DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )". Any reason why you don't use that? – user unknown Mar 8 '12 at 16:22
When doing variable assignment to the output of command substitution or to the output of a variable reference, there is no reason whatsoever to use quotes. Proof: run x="my name is ryran" and then a=$x or a=${x/ryran/nick klauer} or a=$(echo $x) – rsaw Mar 8 '12 at 18:48
Why not just re-use $PWD ? (not worth an 'answer') – laebshade Mar 10 '12 at 0:23
@laebshade: The issue is that this script needs to know what directory the file is in. calling the script from a symlink $PWD will print where the symlink is, not where the source file is. I want to load in sources relative to that directory, and all of the above is just to try to find the symlink's real directory to load other files. – Nick Klauer Mar 12 '12 at 13:33

You're probably missing a newline at the end of your very last line.

share|improve this answer
I'm not sure what you mean, but if I just add another new line, i get -bash: script.sh: line 5: syntax error: unexpected end of file instead of line 4, so did you mean for me to add some \r character somewhere? – Nick Klauer Mar 8 '12 at 19:58
@NickKlauer I don't get an error in bash 4, perhaps there's a bash4 feature that it isn't parsing right – Kevin Mar 8 '12 at 20:05

I cannot reproduce this behavior with bash 3.2.25(2) compiled from source.

There was a change somewhere in the 3.2 series in the way $(…) was parsed.

uuu. Bash now parses command substitutions according to Posix rules: parsing the command contained in $() to find the closing delimiter.

I don't know why I couldn't reproduce it if that's the case, but you might be hitting the old, buggy parsing behavior. Try simplifying the command.

parent=$(dirname "$SOURCE")
DIR=$(cd -P "$parent" && pwd)

I think adding double quotes around the outer command substitution might help. Although double quotes should not make a difference when you use them around the value part of an assignment, I remember a shell version that had a buggy parser that broke on foo=$(something) but coped with foo="$(something)"; this may well have been it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.