Assigning one variable to another in Bash?

Issue

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 = $

Solution

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

For example, to assign the value associated with the variable arg2 to the variable a:

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.

Answered By – Charles Duffy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published