By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,362 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,362 IT Pros & Developers. It's quick & easy.

append a value to an existing variable?

P: 1
Hi all,
I have a basic shell script question.

Say I have a variable called test.

I already have a value in test = 'monkey'

How do I append another value taken from a file, to this variable?
For example, I wanted to get a value called 'dog' from a file called 'animals', and append dog to the test variable, so that it has the values 'dog monkey' in it.

To make it short :
test = 'monkey'

I wanted to get another value (dog) from a file, and append it to test so I get :

test = 'monkey dog'

Appreciate all your help!

Thanks
Jan 8 '08 #1
Share this Question
Share on Google+
2 Replies

radoulov
P: 34
Expand|Select|Wrap|Line Numbers
  1. $ test=monkey
  2. $ echo dog>file
  3. $ test="$test $(<file)"
  4. $ echo "$test"
  5. monkey dog
  6.  
Jan 8 '08 #2

P: 13
Hi all,
I have a basic shell script question.

Say I have a variable called test.

I already have a value in test = 'monkey'

How do I append another value taken from a file, to this variable?
For example, I wanted to get a value called 'dog' from a file called 'animals', and append dog to the test variable, so that it has the values 'dog monkey' in it.

To make it short :
test = 'monkey'

I wanted to get another value (dog) from a file, and append it to test so I get :

test = 'monkey dog'

Appreciate all your help!

Thanks

Another way is to simply redefine the variable and include the original like this:

test="monkey" #${test} = monkey
test="${test} dog" #${test} now = "monkey dog"

This would all be done in memory without using the file on disk and be faster.
Jan 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.