0

Is there a way to loop the commands until the number of names from the prompt, i dont want it to just limit the number of users but to continuisly add names to the prompt while repeating the line of code

read -p "Enter user/s: " user user1 user2 user3 
mkdir $user
mkdir $user1
mkdir $user2
mkdir $user3

Like, Enter user/s: Michael Raphael Gabriel Satanel Helel... (continuosly to infinite) then make different directories named from the list of names from prompt by looping the command instead of copy and pasting

Dominique
  • 16,450
  • 15
  • 56
  • 112

1 Answers1

0

Use read -a arrayname to read into an array, and "${arrayname[@]}" to expand an array to the individual elements it contains.

For mkdir specifically, you can pass more than one directory name to a single invocation and let it do the looping, to not need to loop in shell at all (we use xargs here to split into multiple mkdir invocations when the list of usernames is longer than maximum command-line length):

read -r -a users
printf '%s\0' "${users[@]}" | xargs -0 mkdir --

...but if you do want to do the looping in shell, that would look like:

read -r -a users
for user in "${users[@]}"; do
  mkdir -- "$user"
done
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441