The Nightman
The Nightman

Reputation: 5769

Running a Bash script in multiple directories in Unix

I would like to run some commands within a number of directories as the following code outlines:

    for d in dir*;
    do
        touch $d/file.txt
    done

But while the above code works for directories without spaces in their names, it does not if they do have spaces like: Directory 1 instead of Directory1, each word is treated as a different directory. Any way to overcome this? Thanks.

Upvotes: 0

Views: 430

Answers (1)

that other guy
that other guy

Reputation: 123710

Let's ask shellcheck!

In myscript line 3:
    touch $d/file.txt
          ^-- SC2086: Double quote to prevent globbing and word splitting.

Ok, let's do that:

for d in dir*;
do
    touch "$d"/file.txt
done

and now it works.

Upvotes: 1

Related Questions