bash -- concatenating values from variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash -- concatenating values from variables
# 1  
Old 02-22-2011
bash -- concatenating values from variables

Hi

This is a simple one but I got a lost in translation when doing.

What I want to do, given both variables in the example below, to get one value at the time from both variables, for example:
1:a
2:b

etc...
I need to get this in bash scripting
code:
Code:
varas="1 2 3 4"
varbs="a b c d"
                i=`expr $i + 1`
                var=`echo '$'${i}`
                echo "$storeid:$var"
done

My code does not work as it should but, I have not find the way of getting $1, $2 and to print each value as per the counter, it just prints:
$1
$2
but not the values.

Any idea?

Many thanks
# 2  
Old 02-22-2011
How about this (should work in bash or ksh):

Code:
varas=(1 2 3 4)
varbs=(a b c d)
i=0
while [ $i -lt ${#varas[@]} ]
do
    echo ${varas[i]}:${varbs[i]}
    let i=i+1
done

# 3  
Old 02-22-2011
Chubler_XL,

What makes such a difference using " and ( ) ?

I tested with both and with ( ) the output is the expected but with " I get:

Code:
1 2 3 4:a b c d

Many thanks for your help
# 4  
Old 02-22-2011
Yes, using ( ) defines an array. Where using quotes just assigns a string with spaces.

${#vars[@]} returns a count of items in the array.
# 5  
Old 02-22-2011
Yes, you are totally right there. I was thinking uni-dimensional
many thanks for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenating two mutiline variables in a bash

Hi All, I am having a situation where am capturing results in two variables from an xml file. However, I am looking to print those two variables with pipe in between them and these variable are multi-line. This is how my 1st variable looks like: 20181225010 20190224010 20190224010... (8 Replies)
Discussion started by: svks1985
8 Replies

2. Shell Programming and Scripting

Bash: Setting default values for variables

I have a variable I want to use in bash script. The user will pass an argument to the script and I will store it in `arg_fql`. If the user does not pass the variable, I still never set arg_fql, but I set another variable to a default. However, if the user passes a value, `arg_fql` will be set to... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

Problem while concatenating variables in shell script

Hi folks, I am facing problem when I concat variables with the string. Value for 'JDBC_CLASSES' variable looks malformed (/classes12.zip2.0KAGES) But, my expected result for 'JDBC_CLASSES' is /opt/API-R111/PACKAGES/jdbc/ORACLE9.2.0/classes12.zip Am I missing anything here? My... (10 Replies)
Discussion started by: Adhil
10 Replies

4. Red Hat

Concatenating variables

Hi all, I'm trying to do a very simple script, as you can see as follow: #!/bin/bash #Valorizzazione Token presenti nel file di properties var_path_weblogic="`cat weblogic.properties | grep "dir_wl" | /usr/xpg4/bin/awk '{print $3}'`" var_ip_address="`cat... (5 Replies)
Discussion started by: idro
5 Replies

5. Shell Programming and Scripting

Adding values concatenating values

I have the following script in a shell # The start of a filename file=$(ls -tr $EMT*.dat | tail -1) # Select the latest file echo $file file_seq=$( < /u02/sct/banner/bandev2/xxxxxx/misc/EFTSQL.dat) echo $file_seq file2 = '$file_seq + 1' echo $file2 It is reading a file EFTSQL.dat... (3 Replies)
Discussion started by: rechever
3 Replies

6. Shell Programming and Scripting

Concatenating column values with unique id into single row

Hi, I have a table in Db2 with data say id_1 phase1 id_1 phase2 id_1 phase3 id_2 phase1 id_2 phase2 I need to concatenate the values like id_1 phase1,phase2,phase3 id_2 phase1,phase2 I tried recursive query but in vain as the length of string to be concatenated in quite long. ... (17 Replies)
Discussion started by: jsaravana
17 Replies

7. Shell Programming and Scripting

[BASH] mapping of values from file line into variables

Hello, I've been struggling with this for some time but can't find a way to do it and I haven't found any other similar thread. I'd like to get the 'fields' in a line from a file into variables in just one command. The file contains data with the next structure:... (4 Replies)
Discussion started by: semaler
4 Replies

8. Shell Programming and Scripting

Concatenating Different # of Variables

Hi, I'm quite new at unix and was wondering if anyone could help me with this. I have 2 arrays: eg. STAT=online, STAT=offline, STAT=online WWN=xxxx1, WWN=xxxx2, WWN=xxxx3 I got these information from a script using fcinfo hba-port that runs through a loop. Now, I want to store... (2 Replies)
Discussion started by: jake_won
2 Replies

9. Shell Programming and Scripting

Concatenating values in a File

Hi All, I have a ',' delimited file and i would like concatenate a new value at a specific column. Example :- xXXX,XYZ,20071005,ABC,DEF,123 xXXX,XYZ,20071005,ABC,DEF,123 xXXX,XYZ,20071005,ABC,DEF,123 The output that i want is xXXX,XYZ,20071005001,ABC,DEF,123... (7 Replies)
Discussion started by: amitkhiare
7 Replies

10. Shell Programming and Scripting

Concatenating Variables

FILE_DATE=$(date +"%Y%m%d_%H%M")_ FILE_PREFIX=${FILE_DATE} echo $FILE_PREFIX JS_LOG_DIR="E:\DecisionStream Jobs\Invoice Balance Fact Jobs" echo $JS_LOG_DIR --This is where the problem surfaces. The last line of this script does a rsh to an NT machine and one of the parameters is the... (2 Replies)
Discussion started by: photh
2 Replies
Login or Register to Ask a Question