concatenate variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting concatenate variables
# 1  
Old 02-17-2012
concatenate variables

I need to know how to concatenate variables in Debian. I am making a interactive script where it ask the user for info to add a user I pull the first letter from the first middle and last name into individual variables now i want to put them all in one variable so i can put it into useradd command

This is what happening
Code:
v1= echo $firstname |  cut -b 1
 v2= echo $lastname |  cut -b 1
 v3= echo $middlename |  cut -b 1
 uname= $v1 $v2 $3
 echo $uname

output:
a
d
s

I need the output to look like this
output:
ads

Thanks in advance

Last edited by jim mcnamara; 02-17-2012 at 08:46 PM..
# 2  
Old 02-17-2012
concatenate variables to make username

I need to know how to concatenate variables in Debian. I am making a interactive script where it ask the user for info to add a user I pull the first letter from the first middle and last name into individual variables now i want to put them all in one variable so i can put it into useradd command

This is what happening
Code:
v1= echo $firstname |  cut -b 1
 v2= echo $lastname |  cut -b 1
 v3= echo $middlename |  cut -b 1
 uname= $v1 $v2 $3
 echo $uname

output:
a
d
s

I need the output to look like this
output:
ads

Thanks in advance

Last edited by jim mcnamara; 02-17-2012 at 08:46 PM..
# 3  
Old 02-17-2012
Please use code tags, please do not double post, and consider reading the rules of the forum.
# 4  
Old 02-17-2012
Your shell is bash, this gives the result of the first character from each of three variables concatenated
Code:
result=${firstname,0,1}${middlename,0,1}${lastname,0,1}

# 5  
Old 02-18-2012
If you want to do it like in your own example your left out a couple of things and there were too many spaces:
Code:
v1=$(echo "$firstname"  | cut -b 1)
v2=$(echo "$lastname"   | cut -b 1)
v3=$(echo "$middlename" | cut -b 1)
uname=$v1$v2$v3
echo "$uname"

Jim's example should have : instead of , then it should work
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

3. Shell Programming and Scripting

Concatenate two variables and form the third variable

Hi Guys, I was scratching my head for this for half a day... finally not successful :confused: Following is the problem I have a variable $ var1=123 $ var2-234 $ var3=345 and another Variable $ i=1 Now i wanted to save these into a Variable as shown below for i in 1 2 3 do... (5 Replies)
Discussion started by: ramprabhum
5 Replies

4. Shell Programming and Scripting

Help cannot concatenate Ksh variables ?

Cannot combine these two strings into one line, either as a 3rd variable or echo or printing ? Frustrating. for i in `cat /scripts/pathList.dat` do OldRepo= grep Oldhostname ${i}/.svn/entries | tail -1 NewRepo= grep Oldhostname ${i}/.svn/entries | tail -1 | sed '/Oldhostname/... (41 Replies)
Discussion started by: pcpinkerton
41 Replies

5. Web Development

Concatenate Strings

hi..:) this is my sample part of my program.. $csv_output .= $row.",". $row.",". $row.",". $row.",". $row.",". ... (2 Replies)
Discussion started by: Jeneca
2 Replies

6. Shell Programming and Scripting

Concatenate files

Hi, I want to create a batch(bash) file to combine 23 files together. These files have the same extension. I want the final file is save to a given folder. Once it is done it will delete the 23 files. Thanks for help. Need script. (6 Replies)
Discussion started by: zhshqzyc
6 Replies

7. Shell Programming and Scripting

concatenate variables problem

Hello, I have a tricky problem: I have a $file with a variable number of occurrences of "ORA-" (in this case two) .......... EXP-00008: ORACLE error 3113 encountered ORA-03113: end-of-file on communication channel EXP-00056: ORACLE error 1403 encountered ORA-01403: no data found... (9 Replies)
Discussion started by: Laurentiu
9 Replies

8. Shell Programming and Scripting

Concatenate some of the rows...

i have a file as below and i need to load it into oracle. The problem is, some of the rows are in 2 lines. 123456_PosWlist ----- ----- IN 0/0 123456_PosWListRpt ----- ----- IN 0/0 123456_PosWListCSV ----- -----... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

9. Shell Programming and Scripting

how to concatenate values of two variables with an underscore(_) in between

Hi, I'm new to shell programming. I have two variables a and b a=val1 b=val2 could anyone kindly post the shell script to concatenate the values of variable a and b with an underscore(_) in between? The final output should be val1_val2. (8 Replies)
Discussion started by: badrimohanty
8 Replies

10. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies
Login or Register to Ask a Question