Picking specific array element











up vote
1
down vote

favorite












I have just started reading about "$@" and "$*", I wanted to know if I can specifically point to an element in the "$@" array. Like without using any loop, I want to be able to pick element number 3 from "$@". Is there a way of doing this like "$1+@" or something like this? I already know about "${1}" but want to know specifically about "$@" and "$*". I tried searching for it but did not find anything related to this.










share|improve this question




















  • 1




    array=(apple banana orange); printf "%sn" "${array[1]}"
    – jasonwryan
    Jul 12 '17 at 0:40












  • @jasonwryan Thanks, it worked, doesn't any syntax something specifically like "$1+@" or "$@+1" exist? I think I saw it somewhere, but am not sure because it has been a lot of time.
    – GypsyCosmonaut
    Jul 12 '17 at 1:02

















up vote
1
down vote

favorite












I have just started reading about "$@" and "$*", I wanted to know if I can specifically point to an element in the "$@" array. Like without using any loop, I want to be able to pick element number 3 from "$@". Is there a way of doing this like "$1+@" or something like this? I already know about "${1}" but want to know specifically about "$@" and "$*". I tried searching for it but did not find anything related to this.










share|improve this question




















  • 1




    array=(apple banana orange); printf "%sn" "${array[1]}"
    – jasonwryan
    Jul 12 '17 at 0:40












  • @jasonwryan Thanks, it worked, doesn't any syntax something specifically like "$1+@" or "$@+1" exist? I think I saw it somewhere, but am not sure because it has been a lot of time.
    – GypsyCosmonaut
    Jul 12 '17 at 1:02















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have just started reading about "$@" and "$*", I wanted to know if I can specifically point to an element in the "$@" array. Like without using any loop, I want to be able to pick element number 3 from "$@". Is there a way of doing this like "$1+@" or something like this? I already know about "${1}" but want to know specifically about "$@" and "$*". I tried searching for it but did not find anything related to this.










share|improve this question















I have just started reading about "$@" and "$*", I wanted to know if I can specifically point to an element in the "$@" array. Like without using any loop, I want to be able to pick element number 3 from "$@". Is there a way of doing this like "$1+@" or something like this? I already know about "${1}" but want to know specifically about "$@" and "$*". I tried searching for it but did not find anything related to this.







array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Rui F Ribeiro

38.6k1479128




38.6k1479128










asked Jul 12 '17 at 0:36









GypsyCosmonaut

758928




758928








  • 1




    array=(apple banana orange); printf "%sn" "${array[1]}"
    – jasonwryan
    Jul 12 '17 at 0:40












  • @jasonwryan Thanks, it worked, doesn't any syntax something specifically like "$1+@" or "$@+1" exist? I think I saw it somewhere, but am not sure because it has been a lot of time.
    – GypsyCosmonaut
    Jul 12 '17 at 1:02
















  • 1




    array=(apple banana orange); printf "%sn" "${array[1]}"
    – jasonwryan
    Jul 12 '17 at 0:40












  • @jasonwryan Thanks, it worked, doesn't any syntax something specifically like "$1+@" or "$@+1" exist? I think I saw it somewhere, but am not sure because it has been a lot of time.
    – GypsyCosmonaut
    Jul 12 '17 at 1:02










1




1




array=(apple banana orange); printf "%sn" "${array[1]}"
– jasonwryan
Jul 12 '17 at 0:40






array=(apple banana orange); printf "%sn" "${array[1]}"
– jasonwryan
Jul 12 '17 at 0:40














@jasonwryan Thanks, it worked, doesn't any syntax something specifically like "$1+@" or "$@+1" exist? I think I saw it somewhere, but am not sure because it has been a lot of time.
– GypsyCosmonaut
Jul 12 '17 at 1:02






@jasonwryan Thanks, it worked, doesn't any syntax something specifically like "$1+@" or "$@+1" exist? I think I saw it somewhere, but am not sure because it has been a lot of time.
– GypsyCosmonaut
Jul 12 '17 at 1:02












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










It is said that the positional parameters are not an array.

And the way that exists to set them is via set. No other array needs that.



$ set -- one two t33 f44
$ printf '%sn' "$@"
one
two
t33
f44


But at least in bash (and ksh and zsh), they could be selected just as easy:



$ set -- one two t33 f44
$ echo "${@:2:1}"
two
$ echo "${@:2:2}"
two t33





share|improve this answer





















  • If echo echo ${@:2:1} gives the output two. So does this mean that one two t33 f44 are set at the indexes 1 2 3 4 respectively instead of usual format 0 1 2 3 considered in arrays?
    – GypsyCosmonaut
    Jul 12 '17 at 1:24












  • And also, why does echo ${@:0:1} gives the output similar to echo $0 i.e the current shell name?
    – GypsyCosmonaut
    Jul 12 '17 at 1:30






  • 1




    Yes!. That's the usual numbering of positional parameters as $0 is (most of the time) the name of the running script/program. Just do an echo $0 to see it. @GypsyCosmonaut
    – Arrow
    Jul 12 '17 at 1:31






  • 1




    Well, as the zero index is the name of the executing script/shell, it must be what should be printed by asking for it with echo ${@:0:1}. Doesn't it? @GypsyCosmonaut
    – Arrow
    Jul 12 '17 at 1:33






  • 1




    ${parameter:offset:length} is substring expansion in general, but for @ it is treated differently: ${@:offset:length}you get length positional parameters beginning at offset.
    – NickD
    Jul 12 '17 at 1:34


















up vote
1
down vote













$@ is not an array: it's just a list of the arguments. In bash, you can create an array, initialize it with the values from $@ and then use indexing:



declare -a foo=($@)

echo ${foo[2]}


The array indices start from 0, so the above prints the third argument to the script.






share|improve this answer




























    up vote
    0
    down vote













    Essentially, you can't. The thing is that $* and $@ are not arrays; they are simple variables. Thus, it isn't possible to index them.



    Their values are just strings, defined in slightly different ways. $1, $2, etc. give you access to the individual components.






    share|improve this answer

















    • 1




      I believe that you should take a look to my answer. There is an easy way of using indexes to read elements of the positional parameters (at least on some shells)
      – Arrow
      Jul 12 '17 at 1:17










    • My answer does not conflict with that. I was correcting the assumption in the question that the parameters are arrays, that's all.
      – Bob Eager
      Jul 12 '17 at 6:38











    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%2f377840%2fpicking-specific-array-element%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    It is said that the positional parameters are not an array.

    And the way that exists to set them is via set. No other array needs that.



    $ set -- one two t33 f44
    $ printf '%sn' "$@"
    one
    two
    t33
    f44


    But at least in bash (and ksh and zsh), they could be selected just as easy:



    $ set -- one two t33 f44
    $ echo "${@:2:1}"
    two
    $ echo "${@:2:2}"
    two t33





    share|improve this answer





















    • If echo echo ${@:2:1} gives the output two. So does this mean that one two t33 f44 are set at the indexes 1 2 3 4 respectively instead of usual format 0 1 2 3 considered in arrays?
      – GypsyCosmonaut
      Jul 12 '17 at 1:24












    • And also, why does echo ${@:0:1} gives the output similar to echo $0 i.e the current shell name?
      – GypsyCosmonaut
      Jul 12 '17 at 1:30






    • 1




      Yes!. That's the usual numbering of positional parameters as $0 is (most of the time) the name of the running script/program. Just do an echo $0 to see it. @GypsyCosmonaut
      – Arrow
      Jul 12 '17 at 1:31






    • 1




      Well, as the zero index is the name of the executing script/shell, it must be what should be printed by asking for it with echo ${@:0:1}. Doesn't it? @GypsyCosmonaut
      – Arrow
      Jul 12 '17 at 1:33






    • 1




      ${parameter:offset:length} is substring expansion in general, but for @ it is treated differently: ${@:offset:length}you get length positional parameters beginning at offset.
      – NickD
      Jul 12 '17 at 1:34















    up vote
    1
    down vote



    accepted










    It is said that the positional parameters are not an array.

    And the way that exists to set them is via set. No other array needs that.



    $ set -- one two t33 f44
    $ printf '%sn' "$@"
    one
    two
    t33
    f44


    But at least in bash (and ksh and zsh), they could be selected just as easy:



    $ set -- one two t33 f44
    $ echo "${@:2:1}"
    two
    $ echo "${@:2:2}"
    two t33





    share|improve this answer





















    • If echo echo ${@:2:1} gives the output two. So does this mean that one two t33 f44 are set at the indexes 1 2 3 4 respectively instead of usual format 0 1 2 3 considered in arrays?
      – GypsyCosmonaut
      Jul 12 '17 at 1:24












    • And also, why does echo ${@:0:1} gives the output similar to echo $0 i.e the current shell name?
      – GypsyCosmonaut
      Jul 12 '17 at 1:30






    • 1




      Yes!. That's the usual numbering of positional parameters as $0 is (most of the time) the name of the running script/program. Just do an echo $0 to see it. @GypsyCosmonaut
      – Arrow
      Jul 12 '17 at 1:31






    • 1




      Well, as the zero index is the name of the executing script/shell, it must be what should be printed by asking for it with echo ${@:0:1}. Doesn't it? @GypsyCosmonaut
      – Arrow
      Jul 12 '17 at 1:33






    • 1




      ${parameter:offset:length} is substring expansion in general, but for @ it is treated differently: ${@:offset:length}you get length positional parameters beginning at offset.
      – NickD
      Jul 12 '17 at 1:34













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    It is said that the positional parameters are not an array.

    And the way that exists to set them is via set. No other array needs that.



    $ set -- one two t33 f44
    $ printf '%sn' "$@"
    one
    two
    t33
    f44


    But at least in bash (and ksh and zsh), they could be selected just as easy:



    $ set -- one two t33 f44
    $ echo "${@:2:1}"
    two
    $ echo "${@:2:2}"
    two t33





    share|improve this answer












    It is said that the positional parameters are not an array.

    And the way that exists to set them is via set. No other array needs that.



    $ set -- one two t33 f44
    $ printf '%sn' "$@"
    one
    two
    t33
    f44


    But at least in bash (and ksh and zsh), they could be selected just as easy:



    $ set -- one two t33 f44
    $ echo "${@:2:1}"
    two
    $ echo "${@:2:2}"
    two t33






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 12 '17 at 1:14









    Arrow

    2,460218




    2,460218












    • If echo echo ${@:2:1} gives the output two. So does this mean that one two t33 f44 are set at the indexes 1 2 3 4 respectively instead of usual format 0 1 2 3 considered in arrays?
      – GypsyCosmonaut
      Jul 12 '17 at 1:24












    • And also, why does echo ${@:0:1} gives the output similar to echo $0 i.e the current shell name?
      – GypsyCosmonaut
      Jul 12 '17 at 1:30






    • 1




      Yes!. That's the usual numbering of positional parameters as $0 is (most of the time) the name of the running script/program. Just do an echo $0 to see it. @GypsyCosmonaut
      – Arrow
      Jul 12 '17 at 1:31






    • 1




      Well, as the zero index is the name of the executing script/shell, it must be what should be printed by asking for it with echo ${@:0:1}. Doesn't it? @GypsyCosmonaut
      – Arrow
      Jul 12 '17 at 1:33






    • 1




      ${parameter:offset:length} is substring expansion in general, but for @ it is treated differently: ${@:offset:length}you get length positional parameters beginning at offset.
      – NickD
      Jul 12 '17 at 1:34


















    • If echo echo ${@:2:1} gives the output two. So does this mean that one two t33 f44 are set at the indexes 1 2 3 4 respectively instead of usual format 0 1 2 3 considered in arrays?
      – GypsyCosmonaut
      Jul 12 '17 at 1:24












    • And also, why does echo ${@:0:1} gives the output similar to echo $0 i.e the current shell name?
      – GypsyCosmonaut
      Jul 12 '17 at 1:30






    • 1




      Yes!. That's the usual numbering of positional parameters as $0 is (most of the time) the name of the running script/program. Just do an echo $0 to see it. @GypsyCosmonaut
      – Arrow
      Jul 12 '17 at 1:31






    • 1




      Well, as the zero index is the name of the executing script/shell, it must be what should be printed by asking for it with echo ${@:0:1}. Doesn't it? @GypsyCosmonaut
      – Arrow
      Jul 12 '17 at 1:33






    • 1




      ${parameter:offset:length} is substring expansion in general, but for @ it is treated differently: ${@:offset:length}you get length positional parameters beginning at offset.
      – NickD
      Jul 12 '17 at 1:34
















    If echo echo ${@:2:1} gives the output two. So does this mean that one two t33 f44 are set at the indexes 1 2 3 4 respectively instead of usual format 0 1 2 3 considered in arrays?
    – GypsyCosmonaut
    Jul 12 '17 at 1:24






    If echo echo ${@:2:1} gives the output two. So does this mean that one two t33 f44 are set at the indexes 1 2 3 4 respectively instead of usual format 0 1 2 3 considered in arrays?
    – GypsyCosmonaut
    Jul 12 '17 at 1:24














    And also, why does echo ${@:0:1} gives the output similar to echo $0 i.e the current shell name?
    – GypsyCosmonaut
    Jul 12 '17 at 1:30




    And also, why does echo ${@:0:1} gives the output similar to echo $0 i.e the current shell name?
    – GypsyCosmonaut
    Jul 12 '17 at 1:30




    1




    1




    Yes!. That's the usual numbering of positional parameters as $0 is (most of the time) the name of the running script/program. Just do an echo $0 to see it. @GypsyCosmonaut
    – Arrow
    Jul 12 '17 at 1:31




    Yes!. That's the usual numbering of positional parameters as $0 is (most of the time) the name of the running script/program. Just do an echo $0 to see it. @GypsyCosmonaut
    – Arrow
    Jul 12 '17 at 1:31




    1




    1




    Well, as the zero index is the name of the executing script/shell, it must be what should be printed by asking for it with echo ${@:0:1}. Doesn't it? @GypsyCosmonaut
    – Arrow
    Jul 12 '17 at 1:33




    Well, as the zero index is the name of the executing script/shell, it must be what should be printed by asking for it with echo ${@:0:1}. Doesn't it? @GypsyCosmonaut
    – Arrow
    Jul 12 '17 at 1:33




    1




    1




    ${parameter:offset:length} is substring expansion in general, but for @ it is treated differently: ${@:offset:length}you get length positional parameters beginning at offset.
    – NickD
    Jul 12 '17 at 1:34




    ${parameter:offset:length} is substring expansion in general, but for @ it is treated differently: ${@:offset:length}you get length positional parameters beginning at offset.
    – NickD
    Jul 12 '17 at 1:34












    up vote
    1
    down vote













    $@ is not an array: it's just a list of the arguments. In bash, you can create an array, initialize it with the values from $@ and then use indexing:



    declare -a foo=($@)

    echo ${foo[2]}


    The array indices start from 0, so the above prints the third argument to the script.






    share|improve this answer

























      up vote
      1
      down vote













      $@ is not an array: it's just a list of the arguments. In bash, you can create an array, initialize it with the values from $@ and then use indexing:



      declare -a foo=($@)

      echo ${foo[2]}


      The array indices start from 0, so the above prints the third argument to the script.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        $@ is not an array: it's just a list of the arguments. In bash, you can create an array, initialize it with the values from $@ and then use indexing:



        declare -a foo=($@)

        echo ${foo[2]}


        The array indices start from 0, so the above prints the third argument to the script.






        share|improve this answer












        $@ is not an array: it's just a list of the arguments. In bash, you can create an array, initialize it with the values from $@ and then use indexing:



        declare -a foo=($@)

        echo ${foo[2]}


        The array indices start from 0, so the above prints the third argument to the script.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 12 '17 at 1:02









        NickD

        1,6551313




        1,6551313






















            up vote
            0
            down vote













            Essentially, you can't. The thing is that $* and $@ are not arrays; they are simple variables. Thus, it isn't possible to index them.



            Their values are just strings, defined in slightly different ways. $1, $2, etc. give you access to the individual components.






            share|improve this answer

















            • 1




              I believe that you should take a look to my answer. There is an easy way of using indexes to read elements of the positional parameters (at least on some shells)
              – Arrow
              Jul 12 '17 at 1:17










            • My answer does not conflict with that. I was correcting the assumption in the question that the parameters are arrays, that's all.
              – Bob Eager
              Jul 12 '17 at 6:38















            up vote
            0
            down vote













            Essentially, you can't. The thing is that $* and $@ are not arrays; they are simple variables. Thus, it isn't possible to index them.



            Their values are just strings, defined in slightly different ways. $1, $2, etc. give you access to the individual components.






            share|improve this answer

















            • 1




              I believe that you should take a look to my answer. There is an easy way of using indexes to read elements of the positional parameters (at least on some shells)
              – Arrow
              Jul 12 '17 at 1:17










            • My answer does not conflict with that. I was correcting the assumption in the question that the parameters are arrays, that's all.
              – Bob Eager
              Jul 12 '17 at 6:38













            up vote
            0
            down vote










            up vote
            0
            down vote









            Essentially, you can't. The thing is that $* and $@ are not arrays; they are simple variables. Thus, it isn't possible to index them.



            Their values are just strings, defined in slightly different ways. $1, $2, etc. give you access to the individual components.






            share|improve this answer












            Essentially, you can't. The thing is that $* and $@ are not arrays; they are simple variables. Thus, it isn't possible to index them.



            Their values are just strings, defined in slightly different ways. $1, $2, etc. give you access to the individual components.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 12 '17 at 1:05









            Bob Eager

            1,8861421




            1,8861421








            • 1




              I believe that you should take a look to my answer. There is an easy way of using indexes to read elements of the positional parameters (at least on some shells)
              – Arrow
              Jul 12 '17 at 1:17










            • My answer does not conflict with that. I was correcting the assumption in the question that the parameters are arrays, that's all.
              – Bob Eager
              Jul 12 '17 at 6:38














            • 1




              I believe that you should take a look to my answer. There is an easy way of using indexes to read elements of the positional parameters (at least on some shells)
              – Arrow
              Jul 12 '17 at 1:17










            • My answer does not conflict with that. I was correcting the assumption in the question that the parameters are arrays, that's all.
              – Bob Eager
              Jul 12 '17 at 6:38








            1




            1




            I believe that you should take a look to my answer. There is an easy way of using indexes to read elements of the positional parameters (at least on some shells)
            – Arrow
            Jul 12 '17 at 1:17




            I believe that you should take a look to my answer. There is an easy way of using indexes to read elements of the positional parameters (at least on some shells)
            – Arrow
            Jul 12 '17 at 1:17












            My answer does not conflict with that. I was correcting the assumption in the question that the parameters are arrays, that's all.
            – Bob Eager
            Jul 12 '17 at 6:38




            My answer does not conflict with that. I was correcting the assumption in the question that the parameters are arrays, that's all.
            – Bob Eager
            Jul 12 '17 at 6:38


















            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%2f377840%2fpicking-specific-array-element%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

            サソリ

            広島県道265号伴広島線

            Setup Asymptote in Texstudio