I'm trying to use bash's variable name expansion, but I can't seem to get this to work. ${!${prefix}*} in particular is what is failing. Below is a reproducible example.
#!/bin/bash
MountVolumes_b_mkfs_options='foo bar baz'
MountVolumes_b_path=/foo/bar/baz
MountVolumes_b_mnt_options='foo bar baz'
MountVolumes_b_fs=xfs
MountVolumes_c_path=/foo/bar/baz
MountVolumes_c_fs=xfs
MountVolumes_c_mkfs_fs_options=-'t really -foo /ugly/options'
MountVolumes_c_mkfs_options='-t really -foo /ugly/options'
prefixes=($(echo "${!MountVolumes*}" | grep 'MountVolumes_[b-z]' -o | uniq))
for prefix in ${prefixes[@]}; do
echo "prefix: ${prefix}"
##I need this to expand to:
##MountVolumes_b_mkfs_options MountVolumes_b_path MountVolumes_b_mnt_options MountVolumes_b_fs
echo "${!${prefix}*}"
done
echo "${!MountVolumes_b*}" ##Works
How do I do this?
nameref. I don't have time to give a complete answer, but I think that's what you want.