1

I'm new to bash and I am trying to create a script that should find an archive in a given directory. $1 is the name of archive.

When the given path is ./1/ar.tgz the script works. But when path is ../data 1/01_text.tgz I have the following problem:

dirname: extra operand "1/01_text.tgz"

and then No such file or directory.

Here is my code fragment:

VAR=$1
DIR=$(dirname ${VAR})
cd $DIR

What am I doing wrong?

khelwood
  • 55,782
  • 14
  • 81
  • 108
user2950602
  • 395
  • 1
  • 7
  • 21

2 Answers2

2

Ahmed's answer is right, but you also need to enclose VAR in double quotes. The correct code fragment is:

VAR=$1
DIR=$(dirname "$VAR")
cd "$DIR"
Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
1

The space is causing the problem: cd $DIR gets expanded to cd ../data 1/01_text.tgz and cd doesn't know what to make of the third "argument". Add quotes around the directory: cd "$DIR".

Ahmed Fasih
  • 6,458
  • 7
  • 54
  • 95