Use a variable reference “inside” another variable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
20
down vote

favorite
4












I'm sure it is relatively simple, I just don't know how to do it.



#!/usr/bin/ksh
set `iostat`
myvar=6


I want to something like echo $$myvar which i want interpreted as $$myvar -> $6 -> value










share|improve this question



















  • 3




    The technical term is variable indirection.
    – Thor
    Jun 24 '12 at 13:18















up vote
20
down vote

favorite
4












I'm sure it is relatively simple, I just don't know how to do it.



#!/usr/bin/ksh
set `iostat`
myvar=6


I want to something like echo $$myvar which i want interpreted as $$myvar -> $6 -> value










share|improve this question



















  • 3




    The technical term is variable indirection.
    – Thor
    Jun 24 '12 at 13:18













up vote
20
down vote

favorite
4









up vote
20
down vote

favorite
4






4





I'm sure it is relatively simple, I just don't know how to do it.



#!/usr/bin/ksh
set `iostat`
myvar=6


I want to something like echo $$myvar which i want interpreted as $$myvar -> $6 -> value










share|improve this question















I'm sure it is relatively simple, I just don't know how to do it.



#!/usr/bin/ksh
set `iostat`
myvar=6


I want to something like echo $$myvar which i want interpreted as $$myvar -> $6 -> value







shell-script ksh variable-substitution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 27 '14 at 15:00









mirabilos

1,199826




1,199826










asked Jun 22 '12 at 20:54









Brandon Kreisel

4652514




4652514







  • 3




    The technical term is variable indirection.
    – Thor
    Jun 24 '12 at 13:18













  • 3




    The technical term is variable indirection.
    – Thor
    Jun 24 '12 at 13:18








3




3




The technical term is variable indirection.
– Thor
Jun 24 '12 at 13:18





The technical term is variable indirection.
– Thor
Jun 24 '12 at 13:18











4 Answers
4






active

oldest

votes

















up vote
23
down vote



accepted










You can do this sort of thing with eval, built-in to many fine shells, including ksh:



#!/usr/bin/ksh
set $(iostat)
myvar=6
eval "echo $$myvar"


The trick is to double-quote the string you feed to eval so that $myvar gets substituted with "6", and to backslash the outer dollar-sign, so that eval gets a string "$6".



I got "%user" for the output, but I tried it on a multi-processor RHEL machine.






share|improve this answer
















  • 1




    You are officially the Supreme Exalted Grand Master of the week b/c that even works on the unfathomably awful ksh (really pdksh) in OpenBSD 5.4. If you want to set var vv to the value of the var whose name is in the var vn, just do vv=$( eval "echo $$vn" ). Thanks a ton!
    – execNext
    Apr 5 '14 at 0:07


















up vote
21
down vote













Indirect variable reference



Modern advanced shells have a method to reference the value of a variable whose name is stored in another variable. Unfortunately the method differs between ksh, bash and zsh.



In mksh ≥R39b, you can make myvar a nameref:



typeset -n myvar=6
echo "$myvar"


This doesn't work in ATT ksh93 because it doesn't support namerefs to positional parameters. In the case where you have a variable containing a variable name, you can use this method.



foo=bar
typeset -n myvar=foo
echo "$myvar" # prints bar


In bash ≥2.0, you can write



echo "$!myvar"


In zsh, you can write



echo $(P)myvar


In older shells, including ksh88 and pdksh, your only recourse when you have a variable containing another variable name and want to use the value of this variable eval, as explained by Bruce Ediger. This solution works in any Bourne/POSIX shell.



eval "value=$$myvar"
echo "$value"


Using an array



This is the best method here: it's simpler and more portable.



For your use case, in any shell with arrays (all ksh variants, bash ≥2.0, zsh), you can assign to an array variable and take the element you wish. Beware that ksh and bash arrays start numbering at 0, but zsh starts at 1 unless you issue setopt ksh_arrays or emulate ksh.



set -A iostat -- $(iostat)
echo "$iostat[5]"


If you want to copy the positional parameters to an array variable a:



set -A a -- "$@"


In ksh93, mksh ≥R39b, bash ≥2.0 and zsh, you can use the array assignment syntax:



iostat=($(iostat))
echo "$iostat[5]"





share|improve this answer






















  • Wow, your 'Bourne/POSIX' solution also works in OpenBSD 5.4's ksh/pdksh. To apply it to the example in my comment to Bruce Ediger's answer above, just do eval "vv=$$vn". Merci beaucoup, kind sir.
    – execNext
    Apr 5 '14 at 0:23

















up vote
1
down vote













As indicated by Gilles (who provided the bash part of the answer), also not invalidating Bruce Ediger’s (on how to do it portably with eval), here’s how to do it with nameref in recent mksh (and AT&T ksh93, except – as @Gilles commented – namerefs cannot refer to positional parameters in AT&T ksh, only to named parameters):



#!/bin/mksh
set -- $(iostat)
nameref myvar=6
echo $myvar


Added the -- after set for improved resistence, too.






share|improve this answer






















  • As of ksh 93u, namerefs can't reference positional parameters (typeset: 6: invalid variable name).
    – Gilles
    Feb 27 '14 at 14:35










  • Oh, fun. Thanks for adding!
    – mirabilos
    Feb 27 '14 at 14:36

















up vote
0
down vote













Another use of arrays



Haven't used either ksh or any variant for some time, so I'm not sure if ksh(or bash) has a similar capability. My primary shell is zsh. I use arrays when handling output from commands like iostat because they produce multiple lines, and not all lines are the same format/length.



#! /bin/zsh
IOStatOutput=("$(@f)$(iostat)") # Produces one element per line


The above also bypasses the use of positional parameters. Now, if you want to generate, say, an array of devices:



for Element in 7..$#IOStatOutput # Devices listed in elements 7 thru the last
do
DevList+=( $$=IOStatOutput[Element][1] )
done


I find smaller chunks much easier to handle. You may or may not need to use indirect variable reference, depending on your code. Knowing how it works is still a good thing to know. I use it myself.






share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f41406%2fuse-a-variable-reference-inside-another-variable%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    23
    down vote



    accepted










    You can do this sort of thing with eval, built-in to many fine shells, including ksh:



    #!/usr/bin/ksh
    set $(iostat)
    myvar=6
    eval "echo $$myvar"


    The trick is to double-quote the string you feed to eval so that $myvar gets substituted with "6", and to backslash the outer dollar-sign, so that eval gets a string "$6".



    I got "%user" for the output, but I tried it on a multi-processor RHEL machine.






    share|improve this answer
















    • 1




      You are officially the Supreme Exalted Grand Master of the week b/c that even works on the unfathomably awful ksh (really pdksh) in OpenBSD 5.4. If you want to set var vv to the value of the var whose name is in the var vn, just do vv=$( eval "echo $$vn" ). Thanks a ton!
      – execNext
      Apr 5 '14 at 0:07















    up vote
    23
    down vote



    accepted










    You can do this sort of thing with eval, built-in to many fine shells, including ksh:



    #!/usr/bin/ksh
    set $(iostat)
    myvar=6
    eval "echo $$myvar"


    The trick is to double-quote the string you feed to eval so that $myvar gets substituted with "6", and to backslash the outer dollar-sign, so that eval gets a string "$6".



    I got "%user" for the output, but I tried it on a multi-processor RHEL machine.






    share|improve this answer
















    • 1




      You are officially the Supreme Exalted Grand Master of the week b/c that even works on the unfathomably awful ksh (really pdksh) in OpenBSD 5.4. If you want to set var vv to the value of the var whose name is in the var vn, just do vv=$( eval "echo $$vn" ). Thanks a ton!
      – execNext
      Apr 5 '14 at 0:07













    up vote
    23
    down vote



    accepted







    up vote
    23
    down vote



    accepted






    You can do this sort of thing with eval, built-in to many fine shells, including ksh:



    #!/usr/bin/ksh
    set $(iostat)
    myvar=6
    eval "echo $$myvar"


    The trick is to double-quote the string you feed to eval so that $myvar gets substituted with "6", and to backslash the outer dollar-sign, so that eval gets a string "$6".



    I got "%user" for the output, but I tried it on a multi-processor RHEL machine.






    share|improve this answer












    You can do this sort of thing with eval, built-in to many fine shells, including ksh:



    #!/usr/bin/ksh
    set $(iostat)
    myvar=6
    eval "echo $$myvar"


    The trick is to double-quote the string you feed to eval so that $myvar gets substituted with "6", and to backslash the outer dollar-sign, so that eval gets a string "$6".



    I got "%user" for the output, but I tried it on a multi-processor RHEL machine.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 22 '12 at 21:08









    Bruce Ediger

    34.6k566119




    34.6k566119







    • 1




      You are officially the Supreme Exalted Grand Master of the week b/c that even works on the unfathomably awful ksh (really pdksh) in OpenBSD 5.4. If you want to set var vv to the value of the var whose name is in the var vn, just do vv=$( eval "echo $$vn" ). Thanks a ton!
      – execNext
      Apr 5 '14 at 0:07













    • 1




      You are officially the Supreme Exalted Grand Master of the week b/c that even works on the unfathomably awful ksh (really pdksh) in OpenBSD 5.4. If you want to set var vv to the value of the var whose name is in the var vn, just do vv=$( eval "echo $$vn" ). Thanks a ton!
      – execNext
      Apr 5 '14 at 0:07








    1




    1




    You are officially the Supreme Exalted Grand Master of the week b/c that even works on the unfathomably awful ksh (really pdksh) in OpenBSD 5.4. If you want to set var vv to the value of the var whose name is in the var vn, just do vv=$( eval "echo $$vn" ). Thanks a ton!
    – execNext
    Apr 5 '14 at 0:07





    You are officially the Supreme Exalted Grand Master of the week b/c that even works on the unfathomably awful ksh (really pdksh) in OpenBSD 5.4. If you want to set var vv to the value of the var whose name is in the var vn, just do vv=$( eval "echo $$vn" ). Thanks a ton!
    – execNext
    Apr 5 '14 at 0:07













    up vote
    21
    down vote













    Indirect variable reference



    Modern advanced shells have a method to reference the value of a variable whose name is stored in another variable. Unfortunately the method differs between ksh, bash and zsh.



    In mksh ≥R39b, you can make myvar a nameref:



    typeset -n myvar=6
    echo "$myvar"


    This doesn't work in ATT ksh93 because it doesn't support namerefs to positional parameters. In the case where you have a variable containing a variable name, you can use this method.



    foo=bar
    typeset -n myvar=foo
    echo "$myvar" # prints bar


    In bash ≥2.0, you can write



    echo "$!myvar"


    In zsh, you can write



    echo $(P)myvar


    In older shells, including ksh88 and pdksh, your only recourse when you have a variable containing another variable name and want to use the value of this variable eval, as explained by Bruce Ediger. This solution works in any Bourne/POSIX shell.



    eval "value=$$myvar"
    echo "$value"


    Using an array



    This is the best method here: it's simpler and more portable.



    For your use case, in any shell with arrays (all ksh variants, bash ≥2.0, zsh), you can assign to an array variable and take the element you wish. Beware that ksh and bash arrays start numbering at 0, but zsh starts at 1 unless you issue setopt ksh_arrays or emulate ksh.



    set -A iostat -- $(iostat)
    echo "$iostat[5]"


    If you want to copy the positional parameters to an array variable a:



    set -A a -- "$@"


    In ksh93, mksh ≥R39b, bash ≥2.0 and zsh, you can use the array assignment syntax:



    iostat=($(iostat))
    echo "$iostat[5]"





    share|improve this answer






















    • Wow, your 'Bourne/POSIX' solution also works in OpenBSD 5.4's ksh/pdksh. To apply it to the example in my comment to Bruce Ediger's answer above, just do eval "vv=$$vn". Merci beaucoup, kind sir.
      – execNext
      Apr 5 '14 at 0:23














    up vote
    21
    down vote













    Indirect variable reference



    Modern advanced shells have a method to reference the value of a variable whose name is stored in another variable. Unfortunately the method differs between ksh, bash and zsh.



    In mksh ≥R39b, you can make myvar a nameref:



    typeset -n myvar=6
    echo "$myvar"


    This doesn't work in ATT ksh93 because it doesn't support namerefs to positional parameters. In the case where you have a variable containing a variable name, you can use this method.



    foo=bar
    typeset -n myvar=foo
    echo "$myvar" # prints bar


    In bash ≥2.0, you can write



    echo "$!myvar"


    In zsh, you can write



    echo $(P)myvar


    In older shells, including ksh88 and pdksh, your only recourse when you have a variable containing another variable name and want to use the value of this variable eval, as explained by Bruce Ediger. This solution works in any Bourne/POSIX shell.



    eval "value=$$myvar"
    echo "$value"


    Using an array



    This is the best method here: it's simpler and more portable.



    For your use case, in any shell with arrays (all ksh variants, bash ≥2.0, zsh), you can assign to an array variable and take the element you wish. Beware that ksh and bash arrays start numbering at 0, but zsh starts at 1 unless you issue setopt ksh_arrays or emulate ksh.



    set -A iostat -- $(iostat)
    echo "$iostat[5]"


    If you want to copy the positional parameters to an array variable a:



    set -A a -- "$@"


    In ksh93, mksh ≥R39b, bash ≥2.0 and zsh, you can use the array assignment syntax:



    iostat=($(iostat))
    echo "$iostat[5]"





    share|improve this answer






















    • Wow, your 'Bourne/POSIX' solution also works in OpenBSD 5.4's ksh/pdksh. To apply it to the example in my comment to Bruce Ediger's answer above, just do eval "vv=$$vn". Merci beaucoup, kind sir.
      – execNext
      Apr 5 '14 at 0:23












    up vote
    21
    down vote










    up vote
    21
    down vote









    Indirect variable reference



    Modern advanced shells have a method to reference the value of a variable whose name is stored in another variable. Unfortunately the method differs between ksh, bash and zsh.



    In mksh ≥R39b, you can make myvar a nameref:



    typeset -n myvar=6
    echo "$myvar"


    This doesn't work in ATT ksh93 because it doesn't support namerefs to positional parameters. In the case where you have a variable containing a variable name, you can use this method.



    foo=bar
    typeset -n myvar=foo
    echo "$myvar" # prints bar


    In bash ≥2.0, you can write



    echo "$!myvar"


    In zsh, you can write



    echo $(P)myvar


    In older shells, including ksh88 and pdksh, your only recourse when you have a variable containing another variable name and want to use the value of this variable eval, as explained by Bruce Ediger. This solution works in any Bourne/POSIX shell.



    eval "value=$$myvar"
    echo "$value"


    Using an array



    This is the best method here: it's simpler and more portable.



    For your use case, in any shell with arrays (all ksh variants, bash ≥2.0, zsh), you can assign to an array variable and take the element you wish. Beware that ksh and bash arrays start numbering at 0, but zsh starts at 1 unless you issue setopt ksh_arrays or emulate ksh.



    set -A iostat -- $(iostat)
    echo "$iostat[5]"


    If you want to copy the positional parameters to an array variable a:



    set -A a -- "$@"


    In ksh93, mksh ≥R39b, bash ≥2.0 and zsh, you can use the array assignment syntax:



    iostat=($(iostat))
    echo "$iostat[5]"





    share|improve this answer














    Indirect variable reference



    Modern advanced shells have a method to reference the value of a variable whose name is stored in another variable. Unfortunately the method differs between ksh, bash and zsh.



    In mksh ≥R39b, you can make myvar a nameref:



    typeset -n myvar=6
    echo "$myvar"


    This doesn't work in ATT ksh93 because it doesn't support namerefs to positional parameters. In the case where you have a variable containing a variable name, you can use this method.



    foo=bar
    typeset -n myvar=foo
    echo "$myvar" # prints bar


    In bash ≥2.0, you can write



    echo "$!myvar"


    In zsh, you can write



    echo $(P)myvar


    In older shells, including ksh88 and pdksh, your only recourse when you have a variable containing another variable name and want to use the value of this variable eval, as explained by Bruce Ediger. This solution works in any Bourne/POSIX shell.



    eval "value=$$myvar"
    echo "$value"


    Using an array



    This is the best method here: it's simpler and more portable.



    For your use case, in any shell with arrays (all ksh variants, bash ≥2.0, zsh), you can assign to an array variable and take the element you wish. Beware that ksh and bash arrays start numbering at 0, but zsh starts at 1 unless you issue setopt ksh_arrays or emulate ksh.



    set -A iostat -- $(iostat)
    echo "$iostat[5]"


    If you want to copy the positional parameters to an array variable a:



    set -A a -- "$@"


    In ksh93, mksh ≥R39b, bash ≥2.0 and zsh, you can use the array assignment syntax:



    iostat=($(iostat))
    echo "$iostat[5]"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 13 '17 at 12:37









    Community

    1




    1










    answered Jun 23 '12 at 0:15









    Gilles

    525k12710511578




    525k12710511578











    • Wow, your 'Bourne/POSIX' solution also works in OpenBSD 5.4's ksh/pdksh. To apply it to the example in my comment to Bruce Ediger's answer above, just do eval "vv=$$vn". Merci beaucoup, kind sir.
      – execNext
      Apr 5 '14 at 0:23
















    • Wow, your 'Bourne/POSIX' solution also works in OpenBSD 5.4's ksh/pdksh. To apply it to the example in my comment to Bruce Ediger's answer above, just do eval "vv=$$vn". Merci beaucoup, kind sir.
      – execNext
      Apr 5 '14 at 0:23















    Wow, your 'Bourne/POSIX' solution also works in OpenBSD 5.4's ksh/pdksh. To apply it to the example in my comment to Bruce Ediger's answer above, just do eval "vv=$$vn". Merci beaucoup, kind sir.
    – execNext
    Apr 5 '14 at 0:23




    Wow, your 'Bourne/POSIX' solution also works in OpenBSD 5.4's ksh/pdksh. To apply it to the example in my comment to Bruce Ediger's answer above, just do eval "vv=$$vn". Merci beaucoup, kind sir.
    – execNext
    Apr 5 '14 at 0:23










    up vote
    1
    down vote













    As indicated by Gilles (who provided the bash part of the answer), also not invalidating Bruce Ediger’s (on how to do it portably with eval), here’s how to do it with nameref in recent mksh (and AT&T ksh93, except – as @Gilles commented – namerefs cannot refer to positional parameters in AT&T ksh, only to named parameters):



    #!/bin/mksh
    set -- $(iostat)
    nameref myvar=6
    echo $myvar


    Added the -- after set for improved resistence, too.






    share|improve this answer






















    • As of ksh 93u, namerefs can't reference positional parameters (typeset: 6: invalid variable name).
      – Gilles
      Feb 27 '14 at 14:35










    • Oh, fun. Thanks for adding!
      – mirabilos
      Feb 27 '14 at 14:36














    up vote
    1
    down vote













    As indicated by Gilles (who provided the bash part of the answer), also not invalidating Bruce Ediger’s (on how to do it portably with eval), here’s how to do it with nameref in recent mksh (and AT&T ksh93, except – as @Gilles commented – namerefs cannot refer to positional parameters in AT&T ksh, only to named parameters):



    #!/bin/mksh
    set -- $(iostat)
    nameref myvar=6
    echo $myvar


    Added the -- after set for improved resistence, too.






    share|improve this answer






















    • As of ksh 93u, namerefs can't reference positional parameters (typeset: 6: invalid variable name).
      – Gilles
      Feb 27 '14 at 14:35










    • Oh, fun. Thanks for adding!
      – mirabilos
      Feb 27 '14 at 14:36












    up vote
    1
    down vote










    up vote
    1
    down vote









    As indicated by Gilles (who provided the bash part of the answer), also not invalidating Bruce Ediger’s (on how to do it portably with eval), here’s how to do it with nameref in recent mksh (and AT&T ksh93, except – as @Gilles commented – namerefs cannot refer to positional parameters in AT&T ksh, only to named parameters):



    #!/bin/mksh
    set -- $(iostat)
    nameref myvar=6
    echo $myvar


    Added the -- after set for improved resistence, too.






    share|improve this answer














    As indicated by Gilles (who provided the bash part of the answer), also not invalidating Bruce Ediger’s (on how to do it portably with eval), here’s how to do it with nameref in recent mksh (and AT&T ksh93, except – as @Gilles commented – namerefs cannot refer to positional parameters in AT&T ksh, only to named parameters):



    #!/bin/mksh
    set -- $(iostat)
    nameref myvar=6
    echo $myvar


    Added the -- after set for improved resistence, too.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 27 '14 at 14:37

























    answered Feb 27 '14 at 13:59









    mirabilos

    1,199826




    1,199826











    • As of ksh 93u, namerefs can't reference positional parameters (typeset: 6: invalid variable name).
      – Gilles
      Feb 27 '14 at 14:35










    • Oh, fun. Thanks for adding!
      – mirabilos
      Feb 27 '14 at 14:36
















    • As of ksh 93u, namerefs can't reference positional parameters (typeset: 6: invalid variable name).
      – Gilles
      Feb 27 '14 at 14:35










    • Oh, fun. Thanks for adding!
      – mirabilos
      Feb 27 '14 at 14:36















    As of ksh 93u, namerefs can't reference positional parameters (typeset: 6: invalid variable name).
    – Gilles
    Feb 27 '14 at 14:35




    As of ksh 93u, namerefs can't reference positional parameters (typeset: 6: invalid variable name).
    – Gilles
    Feb 27 '14 at 14:35












    Oh, fun. Thanks for adding!
    – mirabilos
    Feb 27 '14 at 14:36




    Oh, fun. Thanks for adding!
    – mirabilos
    Feb 27 '14 at 14:36










    up vote
    0
    down vote













    Another use of arrays



    Haven't used either ksh or any variant for some time, so I'm not sure if ksh(or bash) has a similar capability. My primary shell is zsh. I use arrays when handling output from commands like iostat because they produce multiple lines, and not all lines are the same format/length.



    #! /bin/zsh
    IOStatOutput=("$(@f)$(iostat)") # Produces one element per line


    The above also bypasses the use of positional parameters. Now, if you want to generate, say, an array of devices:



    for Element in 7..$#IOStatOutput # Devices listed in elements 7 thru the last
    do
    DevList+=( $$=IOStatOutput[Element][1] )
    done


    I find smaller chunks much easier to handle. You may or may not need to use indirect variable reference, depending on your code. Knowing how it works is still a good thing to know. I use it myself.






    share|improve this answer
























      up vote
      0
      down vote













      Another use of arrays



      Haven't used either ksh or any variant for some time, so I'm not sure if ksh(or bash) has a similar capability. My primary shell is zsh. I use arrays when handling output from commands like iostat because they produce multiple lines, and not all lines are the same format/length.



      #! /bin/zsh
      IOStatOutput=("$(@f)$(iostat)") # Produces one element per line


      The above also bypasses the use of positional parameters. Now, if you want to generate, say, an array of devices:



      for Element in 7..$#IOStatOutput # Devices listed in elements 7 thru the last
      do
      DevList+=( $$=IOStatOutput[Element][1] )
      done


      I find smaller chunks much easier to handle. You may or may not need to use indirect variable reference, depending on your code. Knowing how it works is still a good thing to know. I use it myself.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Another use of arrays



        Haven't used either ksh or any variant for some time, so I'm not sure if ksh(or bash) has a similar capability. My primary shell is zsh. I use arrays when handling output from commands like iostat because they produce multiple lines, and not all lines are the same format/length.



        #! /bin/zsh
        IOStatOutput=("$(@f)$(iostat)") # Produces one element per line


        The above also bypasses the use of positional parameters. Now, if you want to generate, say, an array of devices:



        for Element in 7..$#IOStatOutput # Devices listed in elements 7 thru the last
        do
        DevList+=( $$=IOStatOutput[Element][1] )
        done


        I find smaller chunks much easier to handle. You may or may not need to use indirect variable reference, depending on your code. Knowing how it works is still a good thing to know. I use it myself.






        share|improve this answer












        Another use of arrays



        Haven't used either ksh or any variant for some time, so I'm not sure if ksh(or bash) has a similar capability. My primary shell is zsh. I use arrays when handling output from commands like iostat because they produce multiple lines, and not all lines are the same format/length.



        #! /bin/zsh
        IOStatOutput=("$(@f)$(iostat)") # Produces one element per line


        The above also bypasses the use of positional parameters. Now, if you want to generate, say, an array of devices:



        for Element in 7..$#IOStatOutput # Devices listed in elements 7 thru the last
        do
        DevList+=( $$=IOStatOutput[Element][1] )
        done


        I find smaller chunks much easier to handle. You may or may not need to use indirect variable reference, depending on your code. Knowing how it works is still a good thing to know. I use it myself.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 13 '14 at 9:45









        Friartek

        13114




        13114



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f41406%2fuse-a-variable-reference-inside-another-variable%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?