Adding Content to Variable (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding Content to Variable (bash)
# 1  
Old 09-18-2012
Adding Content to Variable (bash)

is this possible?

its kind of like incrementing the value of a number in a variable. but in this case, instead of the value of the variable being a number, it's just contents/strings/characters/alpha-numeric etc. NOT a number.

For instance:

Code:
VAR=Tommy

for all in $(blah blah)

do
    VAR=($echo $all)
    ###the variable $all can be any name .i.e cindy, sally, roy, matt
done

so at the end of the loop, i want the variable VAR to contain everything that was appended to it.

So, if i were to do something like this after the for loop completes:

Code:
echo "$VAR"

i should see everything that was added to the VAR variable:

Code:
Tommy
Cindy
Sally
Roy
Matt

OS: Linux Red Hat / Ubuntu and Sun Solaris
Shell: Bash
# 2  
Old 09-18-2012
Not sure if this is what you want:

Code:
 
VAR=Tommy

for all in $(blah blah)

do
    VAR="$VAR $all"    
done

echo "$VAR"

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 09-18-2012
Recent bash versions support the += operator:

Code:
bash-4.2$ for l in {a..d}; do v+=$l; done; printf '%s\n' "$v"
abcd

This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to send to perl the content of a bash variable as argument?

I want to send the content of a bash variable as a python argument for processing. The problem is that bash doesn't "expand" the content, just sends the expression. script.sh#!/bin/bash export RECEIVEDDATE=$(date +"%Y%m%d.%H%M%S") perl checkdate.py checkdate.pyimport datetime import os... (3 Replies)
Discussion started by: Tribe
3 Replies

2. Shell Programming and Scripting

Adding an element to a bash array with a variable

Hello, I have a simple task and I am having some trouble with the syntax. I have a variable with an assigned value, CMD_STRING='-L 22 -s 0 -r -O -A i -N 100 -n' I would like to add that variable to an array. As far as I have been able to look up, the syntax should be something like, ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

3. UNIX for Dummies Questions & Answers

Adding the content of file in another one...

This might be a weird question, that's not possible, but just wondering: If I have a file for example that has the content: awk '{print "head - "$1" > reduce_lines_"$1}' all And another file that has the content: 221 Is there a way to put the 221 in the space (after head -) of... (27 Replies)
Discussion started by: cosmologist
27 Replies

4. OS X (Apple)

Adding Color to bash

Hey everyone, I have come across an issue to where I am trying to create a script which changes the text color with a simple if then statement. I have seen it done with Fedora 8 but when I try and create it using my MacBook Pro running Snow Leopard it doesn't work. Funny thing is, when I use... (2 Replies)
Discussion started by: dachadster13
2 Replies

5. Shell Programming and Scripting

Bash : Check aphanumeric content in variable

Hello everyone, I'm trying the best way to implement a check on a variable ... in particular I need to assess the content of characters and numbers , I tried on various manuals bash scripting but I could not figure out how to do ... any help? (3 Replies)
Discussion started by: ionral
3 Replies

6. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

7. Shell Programming and Scripting

Content of variable

I have a variable that contains filenames like this: variable="file_1.extension<blank>file_2.extension<blank>file_3.extension<blank>file_4.extension and so on" How can I make filenames to be separated by newline: (I tried Sed but it didn't worked well) file_1.extension file_2.extension... (6 Replies)
Discussion started by: MartyIX
6 Replies

8. Shell Programming and Scripting

Content of Content of a variable!

I got a sample BASH script like this : $ cat test MYVAR=$1 DUMMY1="This is tricky" DUMMY2=24 echo $ $ ./test DUMMY1 ./test: line 5: This is tricky: syntax error in expression (error token is "is tricky") **I was expecting the output as "This is tricky", ah! but no luck **But... (2 Replies)
Discussion started by: jaduks
2 Replies

9. Shell Programming and Scripting

Adding filename into file content

Dear Experts, Please help to teach me how to add the filename into the file content. Actually the file name are EVENTS-20050912. ***************New output that I want*************** EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013 EVENTS-20050912 12:10:28 ALARM: BTSSPAN-297-2... (1 Reply)
Discussion started by: missutoomuch
1 Replies
Login or Register to Ask a Question