Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7809865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:34:09+00:00 2026-06-02T03:34:09+00:00

I’m pretty new to bash scripting and I’m obviously missing some fairly basic understanding

  • 0

I’m pretty new to bash scripting and I’m obviously missing some fairly basic understanding as I can’t work out why this script isn’t doing as I’d expect. I’ve made a simple example reproducing the problem, it is supposed to make 3 folders, with 3 folders inside, then touch a file test inside of each.

Here’s the script:

#!/bin/sh
DIR_LEVEL_1=("1" "2" "3")
for DIR_1 in ${DIR_LEVEL_1[@]}; do
  mkdir $DIR_1
  DIR_LEVEL_2=("$DIR_1/a" "$DIR_1/b" "$DIR_1/c")
  echo $DIR_LEVEL_2
  for DIR_2 in ${DIR_LEVEL2[@]}; do
    mkdir $DIR_2
    touch "$DIR_2/test"
  done
done

The problem I was having was the it wasn’t entering the second for loop, I put in that echo and it looks like the second array isn’t being created properly as the output of the script is:

1/a
2/a
3/a

which is just the first element of the array.

running with -xv flag I get this, which shows it’s taking no notice of that nested for loop (I guess because the array didn’t get created as I wanted?)

#!/bin/sh
DIR_LEVEL_1=("1" "2" "3")
+ DIR_LEVEL_1=("1" "2" "3")
for DIR_1 in ${DIR_LEVEL_1[@]}; do
  mkdir $DIR_1
  DIR_LEVEL_2=("$DIR_1/a" "$DIR_1/b" "$DIR_1/c")
  echo $DIR_LEVEL_2
  for DIR_2 in ${DIR_LEVEL2[@]}; do
    mkdir $DIR_2
    touch "$DIR_2/test"
  done
done
+ for DIR_1 in '${DIR_LEVEL_1[@]}'
+ mkdir 1
+ DIR_LEVEL_2=("$DIR_1/a" "$DIR_1/b" "$DIR_1/c")
+ echo 1/a
1/a
+ for DIR_1 in '${DIR_LEVEL_1[@]}'
+ mkdir 2
+ DIR_LEVEL_2=("$DIR_1/a" "$DIR_1/b" "$DIR_1/c")
+ echo 2/a
2/a
+ for DIR_1 in '${DIR_LEVEL_1[@]}'
+ mkdir 3
+ DIR_LEVEL_2=("$DIR_1/a" "$DIR_1/b" "$DIR_1/c")
+ echo 3/a
3/a

So it only creates the first level of folders

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:34:10+00:00Added an answer on June 2, 2026 at 3:34 am

    Well, the real issue with your script is that you create DIR_LEVEL_2, but you iterate over DIR_LEVEL2. (Note the underscore difference). However, you also neglect to quote "${array[@]}", which means it will not wordsplit properly. All that aside, might I suggest you use brace expansion to make this entire thing a better experience?

    #!/bin/bash
    
    dirs=( {1,2,3}/{a,b,c} )
    mkdir -p "${dirs[@]}"
    for d in "${dirs[@]}"; do
        touch "$d/test"
    done
    

    Edit/postscript: I notice your script above uses #!/bin/sh, but you’ve tagged this post bash. They are not the same thing. Unless you’re certain you want to trade the greater featureset of bash for the greater portability of sh, use #!/bin/bash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.