Shell Script to Join a String
Last Updated :
20 Mar, 2025
There are different ways to concatenate strings in the shell. Some of them are listed below. String concatenation is used in shell scripting to make it much easier for the user to understand and use the program. Appending strings also allow the programmer to learn the working of concatenation on the shell.
These are the ways for joining the string:
- Using String Variables
- Using String Separator
- Using direct String Variable in echo command
- Using += Operator to Join Strings
Shell Scripts
Open a file named "strAppend.sh" in gedit.
# gedit strAppend.sh

Close the editor and Give permission to execute
# chmod 777 strAppend.sh

Using String Variables:
Open a file "strAppend.sh" again and write the below code:
#!/bin/bash
#Using String Variables
echo "********Using String Variables**************"
First_string="Hello"
echo -e "First String is \n ${First_string}"
Second_string="World!"
echo -e "Second String is \n ${Second_string}"
# now concatenate both strings by assigning them to third string
concate_string="$First_string$Second_string"
echo -e "Joined String is \n ${concate_string}"
echo -e "\n\n\n\n\n"
Save the file by "Ctrl + s" and close the file, Now run the file by the below command:
# ./strAppend.sh

Hence, the strings are joined.
Using String Separator
Open a new and empty file named strAppend.sh and write below code
# gedit strAppend.sh
#!/bin/bash
#using String Separator
echo "********Using String Separator**************"
Name="Hemant"
echo -e "First String is \n ${Name}"
roll_no="48"
echo -e "Second String is \n ${roll_no}"
# now concatenate both strings by separator
concate_string=$Name:$roll_no
echo -e "Joined String is \n ${concate_string}"
echo -e "\n\n\n\n\n"
Save the file by "Ctrl + s" and close the file, now run the file by the below command:
# ./strAppend.sh

Hence, the strings are joined.
Using direct String Variable in echo command
Open a new and empty file named strAppend.sh and write below code
# gedit strAppend.sh
#!/bin/bash
# Using Direct Variable in echo command
echo "********Using Direct Variable in echo command**************"
First_string="Hey"
echo -e "First String is \n ${First_string}"
Second_string="You!"
echo -e "Second String is \n ${Second_string}"
# no need of third string
echo "In this no need of third String"
echo -e "Joined String is \n ${First_string}${Second_string}"
echo -e "\n\n\n\n\n"
Save the file by "Ctrl + s" and close the file, now run the file by the below command:
# ./strAppend.sh

Hence, the strings are joined.
Using += Operator to Join Strings
Open a new and empty file named strAppend.sh and write below code
# gedit strAppend.sh
#!/bin/bash
# Using += Operator to Join Strings
echo "********Using += Operator to Join Strings**************"
# First declaring an empty string
Combine=""
#for loop for different strings
for names in 'name1' 'name2' 'name3'; do
# now append using +=
Combine+="$names "
done
echo -e "Joined String is \n ${Combine}"
Save the file by "Ctrl + s" and close the file, now run the file by the below command:
# ./strAppend.sh

Hence, the strings are joined.
Similar Reads
Shell Script to Split a String Shell Scripting or Shell Programming is just like any other programming language. A shell is a special program that provides an interface between the user and the operating system. In Linux/Unix the default shell used is bash and in windows, it is cmd(command prompt). We use the terminal to run a sh
3 min read
Shell Script to Concatenate Two Strings String concatenation is the process of appending a string to the end of another string. This can be done with shell scripting using two methods: using the += operator, or simply writing strings one after the other. The examples below show some shell scripts that can be used to concatenate strings. E
3 min read
String Manipulation in Shell Scripting String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands. Basics of pure bash string manipulation: 1. Assigning content
4 min read
Bash Scripting - Split String In this article, we will discuss how to split strings in a bash script. Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various met
4 min read
How to Concatenate Strings in JavaScript? Here are the various methods to concatenate strings in JavaScript1. Using Template Literals (Template Strings)Template literals (introduced in ES6) provide a simple way to concatenate strings. With template literals, you can embed expressions inside a string using ${} syntax. This method makes the c
3 min read
Reverse a String | Shell Programming In shell scripting, reversing a string can be done using various methods, such as rev, awk, sed, and Perl. Here, we provide examples of reversing a string using different approaches and commands in Unix/Linux systems. We are given a string and we have to use shell script to print it in the reverse o
4 min read