Shell Script to Join a String
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.
Shell Script to Join a String- FAQs
How to join a string in a shell script?
In Bash scripting you can simply join (concatenate) strings using the simple variable expansion or
+=
.string1="Hello"
string2="World"
joined_string="$string1 $string2"
echo "$joined_string"
How do you join two strings in Bash?
To concatenate two strings, just place them next to each other with or without spaces:
string1="Bash"
string2="Scripting"
result="$string1$string2" # Without space
echo "$result"
How to concatenate a string and a variable in Bash?
Use
=
without spaces to combine variables and strings:name="Alice"
greeting="Hello, $name!"
echo "$greeting"
How to concatenate a variable and a string without space in Bash?
Use
${variable}
notation to avoid unintended spaces:file="log"
extension="txt"
filename="${file}.${extension}"
echo "$filename"