0

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.

1 Answer 1

1

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.

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

1 Comment

Perfect, thanks (and thanks for pointing me to shellcheck, never knew about this).

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.