-1

I need to call the $i variable before the $user variable to that the combination of both are called as one variable, if that makes sense. Thank you for any help.

declare -i numusers
echo "How many (non-admin) users? ex: 01 02 10"
read numusers
i=0
while [[ $i -lt $numusers ]] ; do
   echo "enter name of user$i - "
   read user$i
   echo "user$i recored as - $user$i  - "
  (( i += 1 ))
done
  • What does it mean to "**call**" a variable? You can _call_ a function, or _expand_ a variable. BTW: `$user$i` would expand `user` and `i` separately, so you basically get the concatenation of the variable values. – user1934428 Dec 14 '20 at 12:19

1 Answers1

0

I think what your looking for is an array to store you user names in. Does this work for you?

#!/bin/bash

declare -i numusers
echo "How many (non-admin) users? ex: 01 02 10"
read numusers
i=0
while [[ $i -lt $numusers ]] ; do
   echo "enter name of user$i - "
   read user[$i]
   echo "user$i recored as - ${user[$i]}  - "
  (( i += 1 ))
done
bashBedlam
  • 1,402
  • 1
  • 7
  • 11
  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section "Answer Well-Asked Questions", and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Dec 13 '20 at 22:45
  • 1
    BTW, I'd suggest quoting `user[$i]` so it can't be treated as a glob should any matching filenames exist in the current directory, or should the user have the `nullglob`, `failglob` or similar options enabled telling the shell to have non-default behavior in the presence of expressions that look like globs but don't match any files. Better than that, don't bother maintaining an index variable at all; `users=( ); while (( ${#users[@]} < numusers )) && IFS= read -r newUser; do users+=( "$newUser" ); done` – Charles Duffy Dec 13 '20 at 22:46