Error "command not found" when setting value to variable [duplicate] - bash

This question already has answers here:
Indirect variable assignment in bash
(7 answers)
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 6 years ago.
I have the following test.sh script:
#!/bin/bash
foo=0
bar=foo;
${bar}=1
echo $foo;
Output:
./test.sh: line 4: foo=1: command not found
0
Why the "command not found" error? How to change script to "echo $foo" outputs 1?

That's not the way to do indirection unfortunately. To do what you want you could use printf like so
printf -v "$bar" "1"
which will store the value printed (here 1 in the variable name given as an argument to -v which when $bar expands here will be foo
Also, you could use declare like
declare "$bar"=1
which will do variable substitution before executing the declare command.
In your attempt the order of bash processing is biting you. Before variable expansion is done the line is split into commands. A command can include variable assignments, however, at that point you do not have a variable assignment of the form name=value so that part of the command is not treated as an assignment. After that, variable expansion is done and it becomes foo=1 but by then we're done deciding if it's an assignment or not, so just because it now looks like one doesn't mean it gets treated as such.
Since it was not processed as a variable assignment, it must not be treated as a command. You don't have a command named foo=1 in your path, so you get the error of command not found.

You need to use the eval function, like
#!/bin/bash
foo=0
bar=foo;
eval "${bar}=1"
echo $foo;
The ${bar}=1 will first go through the substitution process so it becomes foo=1, and then the eval will evaluate that in context of your shell

Related

Expanded variable as argument of another command via concatenation in Bash [duplicate]

This question already has answers here:
Bash doesn't parse quotes when converting a string to arguments
(5 answers)
Closed 1 year ago.
Imagine I want to call some command some-command via $() with an argument stored in another variable argument, the latter containing space.
With my current understanding, the fact that result="$(some-command $argument)" (e.g. expansion) leads to passing two arguments is as expected.
Question part: why does the result="$(some-command "$argument")" (e.g. concatenation) lead to the desired result of passing one single argument?
More details:
./some-command:
#!/usr/bin/env bash
echo "Arg 1: $1"
echo "Arg 2: $2"
./test-script:
#!/usr/bin/env bash
export PATH="`pwd -P`:$PATH"
argument="one two"
echo "Calling with expansion"
res="$(some-command $argument)"
echo $res
echo "Calling with concatenation"
res="$(some-command "$argument")"
echo $res
Calling test-script leads to the following output:
Calling with expansion
Arg 1: one Arg 2: two
Calling with concatenation
Arg 1: one two Arg 2:
I seem to not grasp when smth is expanded / evaluated and how the expanded variables are grouped into arguments passed to scripts.
Thank you!
P.S. Bonus curiosity is why result="$(some-command \"$argument\")" does not work at all.
That's how quoting and expansions work in bash. In fact, double quotes after = are not needed, as word-splitting is not performed, so
result=$(some-command "$argument")
should work the same way.
There's no "concatenation" going on. Bash treats the string inside $() as a command and runs all the expansions on it before running it.
So, what happens with some-command "$argument"? First, the parameter expansion expands $argument into a string containing spaces. When word-splitting happens, it notes the string was enclosed in double quotes, so it keeps it as a single string.

Bash variable name expansion in a loop [duplicate]

This question already has answers here:
Bash expand variable in a variable
(5 answers)
Closed 1 year ago.
I have several variables that have the name format of:
var0_name
var1_name
var2_name
And I want to be able to loop thru them in a manner like this:
for i in {0..2}
do
echo "My variable name = " ${var[i]_name}
done
I have tried several different ways to get this expansion to work but have had no luck so far.
Using the ${!prefix*} expansion in bash:
#!/bin/bash
var0_name=xyz
var1_name=asdf
var2_name=zx
for v in ${!var*}; do
echo "My variable name = $v"
# echo "Variable '$v' has the value of '${!v}'"
done
or equivalently, by replacing the for loop with:
printf 'My variable name = %s\n' ${!var*}
You may also consider reading the Shell Parameter Expansion for detailed information on all forms of parameter expansions in bash, including those that are used in the answer as well.

assign and expand shell variable in same command line [duplicate]

This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Closed 2 years ago.
I want to assign one or multiple variables at the beginning of a command line in my shell to reuse it in the command invocation. I'm confused of how my shell behaves and want to understand what is happening.
I'm using ZSH but am also interested what the "standard" posix behavior is.
1: % V=/ echo $V # echo is a shell built-in?!?
expected: /. actual: ""
2: % V=/ ls $V # ls is a command
expected: ls /. actual: ls
3: % V=/ ; echo $V
expected: "". actual: /
Here I thought that the semicolon would be equivalent to a new shell line and that I'd need export.
4: % V=/ ; ls $V
expected: ls. actual: ls /
I'm mostly surprised by lines 1 and 2. Is there any ZSH settings that could cause this or do I just start to use a semicolon to use variables in this way?
Variable expansion happens before the command is run, i.e. before the value is assigned to the variable in lines 1 and 2.
export is needed when you need to export the variable to a subshell. A semicolon doesn't introduce a subshell, but causes the assignment to be run before the next command, so the shell now expands the variable by its new value.
Your line 1 would work if you would allow the variable expansion to occur inside the echo and don't force it, before echo gets a chance to run, for instance by
V=/ zsh -c 'echo $V'
or by
V=/ eval 'echo $V'
It doesn't matter that echo is a builtin command. The same idea applies to every command.
Since commands can be separated either by semicolons or by linefeeds, your line 3 is equivalent to
V=/
echo $V
which makes it obvious, why the substitution works in this case.

Bash: assignment of variable on same line not altering echo behavior [duplicate]

This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Closed 2 years ago.
a=2
a=3 echo $a #prints 2
can someone explain why would anyone use the above code in line-2.
a=3 will be ignored as there is no "enter" after it.
But I saw it in script like above and not sure about the purpose.
$a is expanded by the shell (Bash) before a=3 is evaluated. So echo sees its argument as 2, which is what it prints. (If you set -x you can see that what gets executed is a=3 echo 2.)
var=val command is used to set an environment variable to be seen by command during its execution, but nowhere else. So when command reads environment variables (e.g. using getenv()), to it $var is val.
If echo were to look up $a while running, it would have the value 3.
The parent process expands a before the environment is setup in which it sets a different value (3) for a. Despite the fact that variable a set to 3 by the echo executes, the value was expanded already. So it's too late.
You can instead do:
a=3 bash -c 'echo $a'

Use the output of a shell command as a variable [duplicate]

This question already has answers here:
Create variable from string/nameonly parameter to extract data in bash?
(3 answers)
Closed 8 years ago.
I want to use the output of a echo command as variable name. Like,
var1="test"
var2="script"
echo ${$1}
If $1 is var1 echo should print test.
${$1} throws error "bad substitution"
What you want is called variable expansion (or indirect expansion). You have to use the syntax ${!var}:
~$ cat s.sh
var1="test"
var2="script"
echo ${!1}
~$ ./s.sh var1
test
~$ ./s.sh var2
script
From man bash:
${parameter}
The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character which is not to be interpreted as part of its name.
If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[#]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.
You can do this:
$ foo='bar'
$ baz='foo'
$ echo ${!baz}
bar

Resources