16

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
  • 280,126
  • 43
  • 390
  • 441
Murthy
  • 313
  • 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

2 Answers2

24

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.

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 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
  • 1
    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
  • also: bash cares about spaces arg2=$a works arg2 = $a will fail – joseph_morris Feb 01 '23 at 22:40
  • @joseph_morris, obviously. What does that have to do with this answer? `ARG 2 = value` is the **output format** the question is asking for. It's not part of the code that we're running; instead, it's output the code is responsible for printing. – Charles Duffy Feb 01 '23 at 23:16
4

You may also want to alias rather than copy the variable. For example, if you need mutation. Or if you want to run a function multiple times on different variables. Here's how it works

Example:

C=cat
declare -n VAR=C
VAR+=" says Hi"
echo "$C"  # prints "cat says Hi"

Example with arrays/dictionaries:

A=(a a a)
declare -n VAR=A  # "-n" stands for "name", e.g. a new name for the same variable
VAR+=(b)
echo "${A[@]}"  # prints "a a a b"

That is, VAR becomes effectively the same as the original variable. Instead of copying, you're adding an alias. Here's an example with functions:

function myFunc() {
  local -n VAR="$1"
  VAR="Hello from $2"
  echo "I've set variable '$1' to value '$VAR'"
}
myFunc Inbox Bob  # I've set variable 'Inbox' to value 'Hello from Bob'
myFunc Luke Leia  # I've set variable 'Luke' to value 'Hello from Leia'
echo "$Luke"  # Hello from Leia

Whether you should use these approaches is a question. Generally, immutable code is easier to read and to reason about (in almost any programming language). However, sometimes you really need to get stuff done in a certain way. Hope this answer helps you then.

VasiliNovikov
  • 9,681
  • 4
  • 44
  • 62