0

New to Unix not aware of the syntax structure so please excuse my syntax brevity.I am trying to copy a value of a variable and store that in another variable eg:

Two variables:

  1. abc
  2. bcd

Given:

abc=123

I want to copy the contents of abc i.e 123 in bcd. How to achieve this in Unix?

Earlier I was trying to copy the contents of abc in a .txt file which was working for me: see the code snippet below:

abc='123'
echo $abc >>/data/test/tt.txt

But know I want to copy them in another variable so I tried to do the following but was of no success.

    abc='123'
    test=`echo $abc>>bcd`
    echo $test

Can you assist me in this?

3
  • Did you try bcd=$abc ? Commented Dec 8, 2013 at 23:28
  • Hey @vidit that works, i have a scenario where the value of abc will be incremented eg: abc="123" then abc="456" i want bcd to have both the values Commented Dec 8, 2013 at 23:46
  • you expect a variable to have two values? Maybe you should look into Quantum Mechanics. Commented Dec 8, 2013 at 23:50

1 Answer 1

3

Easy:

bcd="$abc"

For example:

abc="hello world"

The quotes there are necessary or else it will try to run a command named world with abc in its environment.

Actually, the quotes are not necessary (thanks to 1_CR for pointing this), but I like to add them for readability:

bcd=$abc
bcd="$abc"

They both do the same, exactly what you need.

Lastly, do not use single quotes, or else you will not get the value of the variable:

bcd='$abc'

Error! Now your bcd variable contains the literal value $abc.

Sign up to request clarification or add additional context in comments.

8 Comments

Quotes not needed in ksh variable assignment apparently
Yes the above works, but what in a scenario where the value of abc will be incremented eg abc="123" then abc="456" now here i want bcd to have both the values which are bcd="123 456" how can we achieve this?
@1_CR: You are right! They are not needed in bash either! You always learn something in SO, even in the apparently easiest questions.
To increment the variable you'll do abc="$abc 456". And here the quotes are necessary. Or if you don't want the space: abc=${abc}456, then you don't need the quotes.
I can do that when i know the what the next incremented value of abc will be like in this case abc="$abc 456" what if we do not knw what the next value will be
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.