why $($var) gives an error?
Clash 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.
linux shell-script shell command-line programming
add a comment |Â
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.
linux shell-script shell command-line programming
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
add a comment |Â
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.
linux shell-script shell command-line programming
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
linux shell-script shell command-line programming
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
add a comment |Â
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
add a comment |Â
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
$
add a comment |Â
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
$
add a comment |Â
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
$
add a comment |Â
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
$
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
$
edited Aug 21 at 10:33
ilkkachu
51.2k678141
51.2k678141
answered Aug 21 at 8:01
steve
12.9k22149
12.9k22149
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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