I am new to bash and just ran into
local name=; name = $("something", "something")
Can someone please explain what =; means?
I have tried to google it, but cannot find any explanation.
CodePudding user response:
You know that ;
is a command separator?
It's the same here. Instead of writing the two commands on a single line, it could be split into two lines:
local name=
name=$("something", "something")
CodePudding user response:
local name=; ...
Define a local variable (i.e. only known to the funcion) named name
and set it to the empty string. The assignment is redundant in this code snippet, since new variables always have the null string assigned.
....; name = $("something", "something")
Invoke a command named name
and pass it several parameters. The first parameter is an equal sign. No special meaning of =
. Note however that we also have:
name=$("something", "something")
Execute a child process, running command something
with parameter something, and assign the standard output of the execution to variable name
; and:
name=something1 something2
which means to execute a command named something2
in an environment, where the environment variable name
is set to something1. In the calling process, name
does not change its value. Therefore,
name=1
name=2 printenv name
echo $name
would print
2
1
CodePudding user response:
=;
does not mean anything. It is a =
followed by a ;
.
The semicolon (;
) is the command separator. A newline does the same thing.
Your code is equivalent to:
local name=
name=...
local
declares a local variable in a function. The first statement declares the local variable name
and initializes it with nothing. The same effect is produced by local name
without any initialization.
Remark
In the question, the second statement has spaces around the =
sign.
The spaces are word separators. With spaces, that line is not an assignment but an invocation of the command name
with two arguments: the =
sign and the output of the command enclosed in $(
and )
.
There should be no spaces around =
for an assignment.