12

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 = $
Charles Duffy
  • 257,635
  • 38
  • 339
  • 400
Murthy
  • 274
  • 1
  • 4
  • 17
  • I edited the title, as the question doesn't appear to have anything to do with declaring variables. (Variables _can_ be declared in shell, but this is necessary to explicitly set types or scope metadata, rather than to assign values). – Charles Duffy Dec 18 '14 at 23:02
  • 1
    Possible duplicate of [Copy values between variables in bash script](https://stackoverflow.com/q/15094968/608639) – jww Mar 12 '19 at 05:47

1 Answers1

19

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.

Charles Duffy
  • 257,635
  • 38
  • 339
  • 400
  • Thanks for the answer. You got it right. Even i thought is that this much simple. But my i am posting my script here. Help me out here.. – Murthy Dec 18 '14 at 23:21
  • @NarayanamurthyBalasubramanian, I would hope that comparing what I'm doing against your script would be enough to make it clear: Change the line assigning ARG2 to `ARG2=$a` -- no extra unnecessary syntax. If this still doesn't answer your question, please clarify what you need to know. – Charles Duffy Dec 18 '14 at 23:29
  • I have changed my question and added few script lines. Help me out on this. – Murthy Dec 18 '14 at 23:31
  • Changing your question after a correct answer is given is not kosher. You should ask a new question in this case. – Charles Duffy Dec 18 '14 at 23:41
  • No worries. Please try to follow the guidelines at http://stackoverflow.com/help/mcve when constructing that question. – Charles Duffy Dec 18 '14 at 23:54
  • @F8ER, correct, but as arrays need a different syntax that's best asked as a separate question. – Charles Duffy Jun 20 '21 at 21:52