You could do some trickery with associative arrays (introduced in Bash 4.0) and namerefs (see manual for declare
and the first paragraph of Shell Parameters – introduced in Bash 4.3):
#!/usr/bin/env bash
declare -A identification0=(
[email]='[email protected]'
[password]='admin123'
)
declare -A identification1=(
[email]='[email protected]'
[password]='passwd1!'
)
declare -n identification
for identification in ${!identification@}; do
echo "Email: ${identification[email]}"
echo "Password: ${identification[password]}"
done
This prints
Email: [email protected]
Password: admin123
Email: [email protected]
Password: passwd1!
declare -A
declares an associative array.
The trick is to assign all your "objects" (associative arrays) variable names starting with the same prefix, like identification
. The ${!prefix@}
notation expands to all variable names starting with prefix
:
$ var1=
$ var2=
$ var3=
$ echo "${!var@}"
var1 var2 var3
Then, to access the key-value pairs of the associative array, we declare the control variable for the for loop with the nameref attribute:
declare -n identification
so that the loop
for identification in ${!identification@}; do
makes identification
behave as if it were the actual variable from the expansion of ${!identification@}
.
In all likelihood, it'll be easier to do something like the following, though:
emails=('[email protected]' '[email protected]')
passwords=('admin123' 'passwd1!')
for (( i = 0; i < ${#emails[@]}; ++i )); do
echo "Email: ${emails[i]}"
echo "Password: ${passwords[i]}"
done
I.e., just loop over two arrays containing your information.
bash
has only one data type: string. Even arrays are simply another form of syntactic quoting, to allow lists of strings containing arbitrary values (i.e., whitespace). (Associative arrays, introduced inbash
4, are slightly better, but still no where near sufficient to allow the types of data structures you are looking for.) – Goveksh93
and later support variables defined as you describe. Unfortunately,ksh
seems to be abandon-ware, as even the man-pages pointed to from kornshell.com are now dead links (and have been for a while). AND I wouldn't be able to point you to documentation on how to use your ksh for that feature. (Its probably out there somewhere). Good luck. – Nasty