Error "mkdir: Missing operand"

Asked

Viewed 90 times

0

I am trying to create a series of folders on my computer that start with strings from "01" to "20".

To do this, I created a list of strings and applied the command mkdir in an iteration:

declare -a names=("02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20")

for i in "${names[@]}"
do
    mkdir $i_txtfiles
done

But this code is not working and returns the error mkdir: missing operand.

I tried using the folder name as string and it didn’t work either. How can I create multiple folders on my computer and name them with the values of a list?

  • mkdir $i_txtfiles, the idea was to generate folders with the name 02_txtfiles, 03_txtfiles, etc.?

  • Yes. Exactly

  • 6

    So I think it should be mkdir $i\_txtfiles, for Bash not to consider _txtfiles as part of the variable name. Or mkdir ${i}_txtfiles

  • Well, it worked. I had tried to create the folders without the "_" and it hadn’t worked either, but now with the escape it worked. Thanks

  • 2

    when it is so just exchange the command for an echo and see what is being printed, serves to test any type of basic interpolation

1 answer

3


The problem is in doing mkdir $i_txtfiles, because Bash is considering the variable $i_txtfiles, which does not exist, so the error says that the function argument is missing mkdir.

To make the desired concatenation, you just need to escape the character _:

mkdir $i\_txtfiles

So Bash understands that the _ is not part of the variable name, searching for the variable $i only.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.