0

I want to create multiple folders using the user input. As an example, if the user has written java,python,react the result that i'm looking for is having then the folders java, python and react respectively. But what happens, is that a folder with the name {java,python,react} is created. The comma is not recognized anymore.

## The code
read -p "External projects: " EXTERNAL_PROJECTS 

mkdir -p {$EXTERNAL_PROJECTS}

1 Answer 1

1

It doesn't work because brace expansion is performed before parameter expansion. That is, after the parameter EXTERNAL_PROJECTS is expanded, the resulting string is not rescanned for brace expansion. One way to solve this problem is to use an array:

IFS=',' read -r -p "External projects: " -a EXTERNAL_PROJECTS

mkdir -p "${EXTERNAL_PROJECTS[@]}"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Nejat . In order to solve my problem, I used a for loop to iterate over the array.
@bmalbusca Surely, if you have a more complex task for each project, you should use a loop like that; for project in "${EXTERNAL_PROJECTS[@]}"; do ...; done. But no loop is needed for a simple mkdir. By the way, the "${EXTERNAL_PROJECTS[*]}" was wrong in the original answer. Updated now. The correct one is "${EXTERNAL_PROJECTS[@]}"

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.