6

I am trying to get the directory where my script is in by using:

scriptdir=`dirname $0`

but this gives me the following error:

dirname: invalid option -- 'b'
Try `dirname --help' for more information.

and I tried what they recommended (i.e dirname --help) and it said command not found. How can I fix this problem?

I am trying to use the scriptdir variable so I can compile the following java program:

java -mx800m -cp "$scriptdir/*" edu.stanford.nlp.parser.lexparser.LexicalizedParser -retainTmpSubcategories -outputFormat "typedDependencies"  -outputFormatOptions "basicDependecies" edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ./sentences/100000.txt > ./parsedsentences/100000.txt
0

2 Answers 2

9

dirname -- "$0"

The -- (dash dash) stops dirname from processing any options in the argument. Always quote $0 in case there are spaces in the name.

3

use quotes. It may fix your issue.

scriptdir=`dirname -- "$0"`

I personnaly prefer this notation in bash scripts, but it is not mandatory:

scriptdir="$(command dirname -- "${0}")"

EDIT:

You may find the answer in the replies to script full name and path $0 not visible when called.

EDIT 2:

Integraded the right answer.

3
  • I added another option to my answer.
    – Biapy
    Jan 28, 2014 at 6:11
  • $0 is '-bash' (ie. a login shell)
    – Ricky
    Jan 28, 2014 at 7:15
  • bash ~ $scriptdir="$(command dirname "${0}")" dirname: opción inválida -- 'b' Try 'dirname --help' for more information. bash ~ $
    – GnP
    Jan 31, 2014 at 20:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .