How to concatenate two strings in Unix shell scripting

How to concatenate two strings in Unix shell scripting
How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"?
Can anybody help me? thanks a lot.

#!/bin/sh season1='summer' season2='winter' echo "[${season1}] + [${season2}] = [${season1}${season2}]"

How to concatenate two strings in Unix shell scripting
Thanks so much!!!

it works great.


Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Concatenate strings in a a for loop

hi guys, I have this question. I am creating an script to that read a text file(.ini) with the list of the patterns to find for example: EPMS_VO EMPS_PARTS Then it check if file have been delivered in a folder and process it with each pattern, but I am having problems concatenting the... (7 Replies)

Discussion started by: Danman

7 Replies

2. Shell Programming and Scripting

Concatenate strings

Hi,I'm trying to concatenate @example.com to each line of a file f1.txt. and push it into f2.txt. Here is the code i'm using. for i in `cat /home/linux1/xxxxxxx/f1.txt`; do echo ${i}@example.com > /home/linux1/xxxxxx/f2.txt; done But above code only printing @example.com in f2.txt. what... (18 Replies)

Discussion started by: sam_bd

18 Replies

3. Programming

Concatenate two strings

Hello! Can anyone explain line 28 for me? I was thinking *a would be replaced by *b, but it actually appends *a to *b. I know it is related to pointer address, but could not figure it out by myself. Thanks a lot! 1 //Concatenate two strings 2 3 #include<stdio.h> 4 char *stradd (char *,... (5 Replies)

Discussion started by: yifangt

5 Replies

4. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)

Discussion started by: garg

6 Replies

5. Shell Programming and Scripting

Concatenate text between patterns in individual strings

In any given file, wherever a certain data block exists I need to concatenate the values(text after each "=" sign) from that block. in that block. The block starts and ends with specific pattern, say BEGIN DS and END DS respectively. The block size may vary. A file will have multiple such blocks.... (12 Replies)

Discussion started by: Prev

12 Replies

6. 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

7. UNIX for Dummies Questions & Answers

concatenate strings

if i use echo "ravi" echo "sankar" it showing output ravi sankar but i want output as ravi sankar remember sankar should be in another echo statement only (2 Replies)

Discussion started by: shankr3

2 Replies

8. UNIX for Dummies Questions & Answers

Concatenate Strings

Hi Friends, I have a requirement I need to concatenate the below two strings. String 1 = /@jobid_at_ String 2 = value stored in ORACLE_SID String 3 = string1 concatenated with String 2. Please let me know how should i do it in UNIX. Thanks, (2 Replies)

Discussion started by: diva_thilak

2 Replies

All times are GMT -4. The time now is 01:30 PM.

Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.

Privacy Policy

Brief: This example will help you to concatenate two or more strings variable in a bash script. This tutorial helps you with multiple shell script examples of concatenating strings in a shell script.

The first example is a general way to concatenate variables of string. You can simply write all the variable one after another:

#!/bin/bash # Shell program to concatenate two strings in variable str1="Welcome " str2="TecAdmin!" str3=$str1$str2 echo $str3

# Shell program to concatenate two strings in variable

Output:

Welcome TecAdmin!

Another Example

You can also use += operator to concatenate two strings and store results in the first string.

#!/bin/bash str1="Welcome " str2="TecAdmin!" str1+=$str2 echo $str1

Output:

Welcome TecAdmin!

One More Example

Use another example with one string variable with user input and another fixed strings.

#!/bin/bash # Shell program to concatenate two strings read -p "Enter your name: " name echo "Welcome $str1"

# Shell program to concatenate two strings

read -p "Enter your name: " name

Script execution result:

Enter your name: Rahul Welcome Rahul

Using {} with Variables

In some situation, you may face issue while concatenating one string with another string variable as shown in below example. Here you need to use curly braces around variable name.

#!/bin/bash filename="backup" echo "$filename_03132018.sql" ### This will not work echo "${filename}_03132018.sql" ### Correct

echo "$filename_03132018.sql"    ### This will not work

echo "${filename}_03132018.sql"  ### Correct

Output

.sql backup_03132018.sql

You can see that first output if incorrect and second output is correct using {} around variable name.

Tags bash, shell, string, variables

Updated on May 25, 2021← Check if two strings are equal?Check if string contains another string? →

Was this article helpful to you? Yes 6 No 2