1

I'm try to create multi folder by combine string and counter. I don't why what is the wrong with my code:

  echo 'Start'
  let count=0
  for p in {1..10}
  do

      DirName= "dir"
      NUM = "${DirName}${count}"
      let count++
      mkdir $NUM
      mkdir "$NUM"/decoded

  done

I got this kind of error

  ./test.sh: line 6: dir: command not found
  ./test.sh: line 7: NUM: command not found

thank in advance

Yeti
  • 1,108
  • 19
  • 28
kaloon
  • 157
  • 4
  • 15

2 Answers2

3

No need to use a loop here. The shell will do all the necessary expansion for you. In fact, you're already relying on the shell to expand {1..10} for you as part of your for loop. So you can just use that expansion directly with mkdir. Also by using mkdir -p <path> (make parent directories as needed), you can avoid having to first do mkdir $NUM before doing mkdir $NUM/decoded.

Putting it all together, you can do what you need in a single line:

mkdir -p dir{1..10}/decoded

Edit: To answer your question more directly regarding the command not found errors, it looks like (as Bjorn A. mentioned) you just need to get rid of the spaces before and after the = in your variable assignments.

Mike Holt
  • 4,452
  • 1
  • 17
  • 24
1

You cannot have spaces around the assignment operator in bash. Lines 6 and 7 must look like:

DirName="dir"
NUM="${DirName}${count}"
Turn
  • 6,656
  • 32
  • 41