append a value to an existing variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bashhead
    New Member
    • Jan 2008
    • 1

    #1

    append a value to an existing variable?

    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
  • radoulov
    New Member
    • Jun 2007
    • 34

    #2
    Code:
    $ test=monkey
    $ echo dog>file
    $ test="$test $(<file)"
    $ echo "$test"
    monkey dog

    Comment

    • bykwzpz
      New Member
      • Jan 2008
      • 12

      #3
      Originally posted by bashhead
      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.

      Comment

      Working...