What does this line of code mean
[ -n "$local_var" ] && eval $1=\$local_var
I can't seem to understand it.
What does this line of code mean
[ -n "$local_var" ] && eval $1=\$local_var
I can't seem to understand it.
[ -n "$var" ] evaluates to true (i.e. return exit status 0) if the length of $var is non-zero. Also cmd1 && cmd2 executes cmd2 only if cmd1 returns an exit status of zero. And cmd1 && cmd2 executes cmd2 only if cmd1 returns a non-zero exit status. Perhaps an example can help illustrate what your code is doing:
#!/usr/bin/env bash
# $local_var has not been initialized so has length zero
[ -n "$local_var" ] || echo '$local_var' has zero length
# Prints: $local_var has zero length
var=foo
local_var=bar
[ -n "$local_var" ] && eval $var=\$local_var
# Above line is equivalent to:
# [ -n "$local_var" ] && foo=$local_var
echo "$var $foo $local_var"
# Prints: foo bar bar
$var=\$local_var and $var='$'local_var the same statement isn't it?$var="$"local_var). But I had only seen $var=\$local_var used before.$ on the first parsing pass. $var='$local_var' or even $var'=$local_var' would work as well.eval $var=\$local_var === foo=$local_varman bash: "The args are read and concatenated together into a single command. This command is then read and executed by the shell". I'd suggest reading: unix.stackexchange.com/a/23117/281661