Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning one variable to another in Bash?

Tags:

linux

shell

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 = $
like image 431
Murthy Avatar asked Dec 18 '14 22:12

Murthy


Video Answer


1 Answers

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.

like image 51
Charles Duffy Avatar answered Oct 12 '22 16:10

Charles Duffy