why $($var) gives an error?

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











up vote
1
down vote

favorite












I am trying to execute the following code in shell but I am not getting the output as expected. The command is as follows:



i=1
echo $($i)


I am getting an error:



Command not found error


I think it should show the value of the first command line argument, rather it is giving error.










share|improve this question



















  • 1




    @steve answers your question nicely, but if you're trying to get access to arguments dynamically, you can look at $@, shift, getopts and bash arrays.
    – mikst
    Aug 21 at 8:37










  • Accessing a positional parameter through a variable
    – Mark Plotnick
    Aug 21 at 10:42















up vote
1
down vote

favorite












I am trying to execute the following code in shell but I am not getting the output as expected. The command is as follows:



i=1
echo $($i)


I am getting an error:



Command not found error


I think it should show the value of the first command line argument, rather it is giving error.










share|improve this question



















  • 1




    @steve answers your question nicely, but if you're trying to get access to arguments dynamically, you can look at $@, shift, getopts and bash arrays.
    – mikst
    Aug 21 at 8:37










  • Accessing a positional parameter through a variable
    – Mark Plotnick
    Aug 21 at 10:42













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to execute the following code in shell but I am not getting the output as expected. The command is as follows:



i=1
echo $($i)


I am getting an error:



Command not found error


I think it should show the value of the first command line argument, rather it is giving error.










share|improve this question















I am trying to execute the following code in shell but I am not getting the output as expected. The command is as follows:



i=1
echo $($i)


I am getting an error:



Command not found error


I think it should show the value of the first command line argument, rather it is giving error.







linux shell-script shell command-line programming






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 21 at 10:12









perror

1,83141833




1,83141833










asked Aug 21 at 7:52









jeffery3015

61




61







  • 1




    @steve answers your question nicely, but if you're trying to get access to arguments dynamically, you can look at $@, shift, getopts and bash arrays.
    – mikst
    Aug 21 at 8:37










  • Accessing a positional parameter through a variable
    – Mark Plotnick
    Aug 21 at 10:42













  • 1




    @steve answers your question nicely, but if you're trying to get access to arguments dynamically, you can look at $@, shift, getopts and bash arrays.
    – mikst
    Aug 21 at 8:37










  • Accessing a positional parameter through a variable
    – Mark Plotnick
    Aug 21 at 10:42








1




1




@steve answers your question nicely, but if you're trying to get access to arguments dynamically, you can look at $@, shift, getopts and bash arrays.
– mikst
Aug 21 at 8:37




@steve answers your question nicely, but if you're trying to get access to arguments dynamically, you can look at $@, shift, getopts and bash arrays.
– mikst
Aug 21 at 8:37












Accessing a positional parameter through a variable
– Mark Plotnick
Aug 21 at 10:42





Accessing a positional parameter through a variable
– Mark Plotnick
Aug 21 at 10:42











1 Answer
1






active

oldest

votes

















up vote
2
down vote













Because the construct $(...) is a command substitution, so $($i) means to run the contents of variable $i. In your case, it would try to execute 1, which the shell cannot find.



See below how it fails, but if we create a script named '1', it works.



Example:



$ i=1
$ echo "$($i)"
-bash: 1: command not found
$ cat >1
#!/bin/bash
echo Hello World
$ chmod 755 1
$ PATH=$PATH:`pwd`
$ echo "$($i)"
Hello World
$


To display the value of the first command line argument, you need $1 or $1



$ cat >mytest
#!/bin/bash
echo "arg 1 is $1"
echo "arg 1 is $1"
$ chmod 755 mytest
$ ./mytest foo
arg 1 is foo
arg 1 is foo
$





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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f463798%2fwhy-var-gives-an-error%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Because the construct $(...) is a command substitution, so $($i) means to run the contents of variable $i. In your case, it would try to execute 1, which the shell cannot find.



    See below how it fails, but if we create a script named '1', it works.



    Example:



    $ i=1
    $ echo "$($i)"
    -bash: 1: command not found
    $ cat >1
    #!/bin/bash
    echo Hello World
    $ chmod 755 1
    $ PATH=$PATH:`pwd`
    $ echo "$($i)"
    Hello World
    $


    To display the value of the first command line argument, you need $1 or $1



    $ cat >mytest
    #!/bin/bash
    echo "arg 1 is $1"
    echo "arg 1 is $1"
    $ chmod 755 mytest
    $ ./mytest foo
    arg 1 is foo
    arg 1 is foo
    $





    share|improve this answer


























      up vote
      2
      down vote













      Because the construct $(...) is a command substitution, so $($i) means to run the contents of variable $i. In your case, it would try to execute 1, which the shell cannot find.



      See below how it fails, but if we create a script named '1', it works.



      Example:



      $ i=1
      $ echo "$($i)"
      -bash: 1: command not found
      $ cat >1
      #!/bin/bash
      echo Hello World
      $ chmod 755 1
      $ PATH=$PATH:`pwd`
      $ echo "$($i)"
      Hello World
      $


      To display the value of the first command line argument, you need $1 or $1



      $ cat >mytest
      #!/bin/bash
      echo "arg 1 is $1"
      echo "arg 1 is $1"
      $ chmod 755 mytest
      $ ./mytest foo
      arg 1 is foo
      arg 1 is foo
      $





      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        Because the construct $(...) is a command substitution, so $($i) means to run the contents of variable $i. In your case, it would try to execute 1, which the shell cannot find.



        See below how it fails, but if we create a script named '1', it works.



        Example:



        $ i=1
        $ echo "$($i)"
        -bash: 1: command not found
        $ cat >1
        #!/bin/bash
        echo Hello World
        $ chmod 755 1
        $ PATH=$PATH:`pwd`
        $ echo "$($i)"
        Hello World
        $


        To display the value of the first command line argument, you need $1 or $1



        $ cat >mytest
        #!/bin/bash
        echo "arg 1 is $1"
        echo "arg 1 is $1"
        $ chmod 755 mytest
        $ ./mytest foo
        arg 1 is foo
        arg 1 is foo
        $





        share|improve this answer














        Because the construct $(...) is a command substitution, so $($i) means to run the contents of variable $i. In your case, it would try to execute 1, which the shell cannot find.



        See below how it fails, but if we create a script named '1', it works.



        Example:



        $ i=1
        $ echo "$($i)"
        -bash: 1: command not found
        $ cat >1
        #!/bin/bash
        echo Hello World
        $ chmod 755 1
        $ PATH=$PATH:`pwd`
        $ echo "$($i)"
        Hello World
        $


        To display the value of the first command line argument, you need $1 or $1



        $ cat >mytest
        #!/bin/bash
        echo "arg 1 is $1"
        echo "arg 1 is $1"
        $ chmod 755 mytest
        $ ./mytest foo
        arg 1 is foo
        arg 1 is foo
        $






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 21 at 10:33









        ilkkachu

        51.2k678141




        51.2k678141










        answered Aug 21 at 8:01









        steve

        12.9k22149




        12.9k22149



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463798%2fwhy-var-gives-an-error%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay