Open In App

Shell Script to Join a String

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

  1. Using String Variables
  2. Using String Separator
  3. Using direct String Variable in echo command
  4. Using += Operator to Join Strings

Shell Scripts

Open a file named “strAppend.sh” in gedit.

# gedit strAppend.sh

Shell Script to Join a String

Close the editor and Give permission to execute

# chmod 777 strAppend.sh

Shell Script to Join a String

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

Shell Script to Join a String

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

Shell Script to Join a String

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

Shell Script to Join a String

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

Shell Script to Join a String

Hence, the strings are joined.


Last Updated : 25 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads