[SOLVED] set a variable to a combination of two other variables
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When you assign variable c, the shell expands the values of a and b into c. If the value of a is changed later on the script, the variable c will have the previous value of a.
You'll have to perform the arithmetic on a and b first, then assign them to c:
Code:
a=0
b=0
a=$((a+1)) # a is 1
b=$((b+2)) # b is 2
c="${a}_${b}" # c is 1_2
thank you very much for your reply.
@Diantre: I have quite a long script and a & b are updated very often, that is why I want to define c once at the beginning. But worst case I will follow that idea.
@crts:eval works well in this example, but I actually have a little more complicated script. Since I thought the problem must be about how I define the variable c (not about how I call the variable c), I did not include in previous post my 4. variable var, which is defined with awk function.
Code:
a=0
b=0
c='${a}_${b}'
let b="$b+1"
var=$(awk -v c=${c} '$3 == c{print $2}' textfile)
echo $var
#in this case I dont get the correct entry on the textfile
Is it possible to get an updated version of variable c in awk function?
c='${a}_${b}' sets c to the literal ${a}_${b}. The variables would be substituted if the were within double quotes: c="${a}_${b}"
Hi catkin,
that is exactly not what the OP wants. He wants to "emulate" a pointer to $a and $b, so that $c will reflect changes of either one of them dynamically.
that is exactly not what the OP wants. He wants to "emulate" a pointer to $a and $b, so that $c will reflect changes of either one of them dynamically.
Thanks for correcting my lazy reading of the thread crts
Shells do not support deferred evaluation, as far as I know; there is no way to define a variable that refers to other variables, with the references resolved at use time (and not at definition time).
I do know of two mechanisms you can use to emulate such variables, though. Assuming you use Bash or a POSIX shell (dash, for example):
Use a function that outputs the desired expression.
Instead of $name or ${name} you have to use $(name) (or `name`.
Code:
c () {
echo -n "${a}_${b}"
}
# Use $(c) or `c` instead of ${c} or $c.
Define a helper variable as the assignment expression. Evaluate the helper variable whenever the components may have changed, before using the assigned variable.
While you can use the variable normally, you'll need to eval "$Update" (if Update contained the helper assignment expression) before using the variable in an expression, to make sure all changes have propagated properly:
Code:
Update='c="${a}_${b}"'
# Run
# eval "$Update"
# before using $c or ${c} to make sure all
# changes in a and b are reflected in c.
It is often easier to modify the code that modifies the constituent variables, and add a "side effect" that updates the derivative variables. This is also surprisingly efficient, only adding a function call overhead to each assignment:
Code:
EvalC='C="${A}_${B}"'
SetAB () {
A="$1"
B="$2"
eval "$EvalC"
export A B C
}
SetA () {
A="$*"
eval "$EvalC"
export A B C
}
SetB () {
B="$*"
eval "$EvalC"
export A B C
}
# To set A to "Foo Bar" and B to "Alice-Bob", use
# SetAB "Foo Bar" "Alice-Bob"
# or
# SetA "Foo Bar"
# SetB "Alice-Bob"
# and C will always reflect the changes automatically.
There are obviously many variants of the above, and a number of tweaks you can do to make any of the above schemes better fit your needs.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.