Skip to main content
added 931 characters in body
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11

Setup:

export VAR_ONE=thisIsVarOne

An alternative using an associative array:

$ unset      newvar
$ declare -A newvar

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

$ echo "${newvar[${VAR_ONE}]}"
somePrefixthisIsVarOne

In a comment OP has stated the need to reference multiple variables, eg, newvar${VAR_ONE}, newvar${OTHER_VAR} and newvar${ANOTHER_VAR}.

This approach with the associative array will work as long as the 3 variables (VAR_ONE, OTHER_VAR and ANOTHER_VAR) have different values, otherwise duplicate values will lead to a single array entry being created (with the latest assignment overwriting the previous assignment).

If the 3 variables could have the same value then another approach would be to use literals for the associative array's indices, eg:

#### instead of:

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

#### we use:

$ newvar[VAR_ONE]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([VAR_ONE]="somePrefixthisIsVarOne" )

Setup:

export VAR_ONE=thisIsVarOne

An alternative using an associative array:

$ unset      newvar
$ declare -A newvar

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

$ echo "${newvar[${VAR_ONE}]}"
somePrefixthisIsVarOne

Setup:

export VAR_ONE=thisIsVarOne

An alternative using an associative array:

$ unset      newvar
$ declare -A newvar

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

$ echo "${newvar[${VAR_ONE}]}"
somePrefixthisIsVarOne

In a comment OP has stated the need to reference multiple variables, eg, newvar${VAR_ONE}, newvar${OTHER_VAR} and newvar${ANOTHER_VAR}.

This approach with the associative array will work as long as the 3 variables (VAR_ONE, OTHER_VAR and ANOTHER_VAR) have different values, otherwise duplicate values will lead to a single array entry being created (with the latest assignment overwriting the previous assignment).

If the 3 variables could have the same value then another approach would be to use literals for the associative array's indices, eg:

#### instead of:

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

#### we use:

$ newvar[VAR_ONE]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([VAR_ONE]="somePrefixthisIsVarOne" )
added 1 character in body
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11

Setup:

export VAR_ONE=thisIsVarOne

An alternative using an associative array:

$ unset      newvar
$ declare -A newvar

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

$ echo "${newvar[${VAR_ONE}]}"
somePrefixthisIsVarOne

Setup:

export VAR_ONE=thisIsVarOne

An alternative using an associative array:

$ unset     newvar
$ declare -A newvar

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

$ echo "${newvar[${VAR_ONE}]}"
somePrefixthisIsVarOne

Setup:

export VAR_ONE=thisIsVarOne

An alternative using an associative array:

$ unset      newvar
$ declare -A newvar

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

$ echo "${newvar[${VAR_ONE}]}"
somePrefixthisIsVarOne
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11

Setup:

export VAR_ONE=thisIsVarOne

An alternative using an associative array:

$ unset     newvar
$ declare -A newvar

$ newvar[${VAR_ONE}]="somePrefix${VAR_ONE}"

$ typeset -p newvar
declare -A newvar=([thisIsVarOne]="somePrefixthisIsVarOne" )

$ echo "${newvar[${VAR_ONE}]}"
somePrefixthisIsVarOne