0

I'm trying to make directories using a variable in the directory name from a variables in an array, but it doesn't seem to concatenate the streams like I thought they would. I've tried a couple different ways.

I'm trying to get three directories named: test_6_bash_with_directory, test_8_bash_with_directory, test_10_bash_with_directory

variable=`seq 6 2 10`

for i in "${variable[@]}"
do
   :
   directory_name="./test_${i}_bash_with_directory"
   mkdir $directory_name
   echo $i
done

This gives me three directories test_6, 8 and 10_bash_with_directory. Replacing ${i} with ${variable} has the same result.

I'd also tried having the mkdir call in the same line as the directory concatenation:

mkdir "./test_${i}_bash_with_directory"

and I got one directory called test_6?8?10_bash_with_directory

So, how do I write this correctly? Thank you for replies!

1 Answer 1

4

Your problem is that variable is a string, not an array. You want:

variable=(`seq 6 2 10`)

for i in "${variable[@]}"
do
   directory_name="./test_${i}_bash_with_directory"
   mkdir $directory_name
   echo $i
done

Note the (...) around your sequence of values.

Sign up to request clarification or add additional context in comments.

2 Comments

Eventually, since it is Bash: variable=({6..10..2}) and mkdir -p -- "$directory_name". Well the bracket expression or the seq command could be replaced by its constants variable=(6 8 10).
So the reason I wanted to use a range of variables is because I'm later hoping to run a program with a different parameter, and then make a directory with that parameter in the name for the output of that program, so I want to keep the range array if possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.