Shell Script to Join a String

25 Mar, 2021
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 VariablesUsing String SeparatorUsing direct String Variable in echo commandUsing += Operator to Join Strings
Using String Variables
Using String Separator
Using direct String Variable in echo command
Using += Operator to Join Strings
Open a file named “strAppend.sh” in gedit.
# gedit strAppend.sh
Close the editor and Give permission to execute
# chmod 777 strAppend.sh
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.
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.
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.
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.
Picked
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
scp command in Linux with Examples
chown command in Linux with Examples
SED command in Linux | Set 2
mv command in Linux with examples
nohup Command in Linux with Examples
chmod command in Linux with examples
Introduction to Linux Operating System
Array Basics in Shell Scripting | Set 1
Basic Operators in Shell Scripting