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
  • 52,115
  • 13
  • 74
  • 94
user2950602
  • 395
  • 1
  • 5
  • 20

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,250
  • 4
  • 39
  • 61
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,421
  • 5
  • 50
  • 92