Assigning One Variable to Another in Bash?

Assigning One Variable to Another in Bash?

I have a doubt. When i declare a value and assign to some variable, I don't know how to reassign the same value to another variable. See the code snippet below.

#/bin/sh    
#declare ARG1 to a
a=ARG1
#declaring $a to ARG2    
ARG2=$`$a`

echo "ARG 2 = $ARG2"

It should display my output as

ARG 2 = ARG1

...but instead the actual output is:

line 5: ARG1: command not found
ARG 2 = $
2

1 Answer

To assign the value associated with the variable arg2 to the variable a, you need simply run dest=$source:

a=ARG1
arg2=$a
echo "ARG 2 = $arg2"

The use of lower-case variable names for local shell variables is by convention, not necessity -- but this has the advantage of avoiding conflicts with environment variables and builtins, both of which use all-uppercase names by convention.

6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.