Linux bash add string to string

How to Concatenate Strings in Bash

Learn to concatenate strings in bash shell scripts. You’ll also learn to append to existing strings and combine strings and integers.

Concatenating strings can be an important part of using any programming language for practical applications.

You can concatenate strings in bash as well. There is no concatenation operator here. Just write the strings one after another to join strings in Bash.

Don’t worry! I’ll show you various actual examples to concatenate strings in bash.

Assigning concatenated strings

There are no data types in Bash like you have in most programming languages. But you can still declare variables in Bash.

You can use printf command to print the value of this string variable:

Let’s create some more strings:

[email protected]:~$ t='To' [email protected]:~$ l='Linux' [email protected]:~$ h='Handbook!' 

I want to combine all these string variables into a single one. How to do that?

In this manner, I have concatenated all four strings into a single variable and named it tony . Do note that I have added a space between the variables.

Let’s quickly confirm that the strings have been combined:

[email protected]:~$ printf "$tony\n" Welcome To Linux Handbook! 

Here’s all of it in a Bash Script:

#!/bin/bash w='Welcome' t='To' l='Linux' h='Handbook' tony="$ $ $ $" printf "$\n" 

Make it executable and run it as a script:

[email protected]:~$ chmod +x concat.sh [email protected]:~$ ./concat.sh Welcome To Linux Handbook! 

The curly braces <> around the variable names are not mandatory while concatenating strings. However, to make things clear and protect it from surrounding characters, it is good practice to wrap them in <>.

Append to string in bash

The above example combines different strings into one.

Let’s take another scenario. Say, you want to append to an already existing string. How to do that? You use the wonderful += operator.

Can you guess the new value of str ? Yes! It is ironman .

[email protected]:~$ str="iron" [email protected]:~$ str+="man" [email protected]:~$ echo $str ironman 

This is helpful when you are using loops in bash. Take this for loop for example:

#!/bin/bash var="" for color in 'Black' 'White' 'Brown' 'Yellow'; do var+="$ " done echo "$var"

If you run the above script, it will append to the string after each iteration.

Concatenate numbers and strings

As I mentioned previously, there are no data types in Bash. Strings and integers are the same and hence they can be easily joined in a single string.

Читайте также:  Alt linux часовой пояс

Let us look at another example through a second script. This time, I’ll use a number:

#!/bin/bash we='We' lv='Love' y='You' morgan=3000 stark="$ $ $ $. " printf "$\n" 
[email protected]:~$ chmod +x morgan.sh [email protected]:~$ ./morgan.sh We Love You 3000. 

Nested concatenation of strings

You can also store those two concatenated strings inside a third one through nested concatenation:

#!/bin/bash w='Welcome' t='To' l='Linux' h='Handbook' tony="$ $ $ $" we='We' lv='Love' y='You' morgan=3000 stark="$ $ $ $. " ironman="$..$" printf "$ Forever!\n"

When you run this shell script, you’ll see this output:

Welcome To Linux Handbook..We Love You 3000. Forever!

I hope this quick little tutorial helped you in concatenating bash strings. If you have questions or suggestions, feel free to leave a comment below.

Источник

String concatenation in bash

The way of joining two or more strings together is called string concatenation. It is a common requirement of any programming language. A specific character or built-in function is used to do the concatenation operation in the standard programming language. But, there is no built-in function in bash like other languages to combine string data or variables. The string data can be combined easily in bash by placing one after another or by using shorthand operator. How string concatenation can be done in bash is shown in this tutorial by using several examples.

Example-1: String variables one after another

The most simple way to join two or more strings together is to place the strings one after another. Create a file named ‘concat1.sh’ and add the following code to combine strings. Two variables, $string1 and $string2 are initialized with string data and stored in another variable, $string3. The value of $string3 is printed by combining another string data.

#!/bin/bash
#Declare first string
string1 = «Welcome»
#Declare second string
string2 = » everyone »
#Combine first and second string
string3 = $string1 $string2
# Print the third string by combining with other string
echo » $string3 to our site»

Run the script by bash command.

The following output will appear after combining all data.

Example-2: String variable after and before the string data

The string variable can be added in any position of the string data. Create a file named ‘concat2.sh’ and add the following code. Here, a string variable is initialized and printed by adding the variable in the middle of the other string.

#!/bin/bash
#Declare string variable
string = «Programming»
#Add the variable in the middle of the string
echo «Bash $string Language»

Run the script by bash command.

The following output will appear after executing the script.

Example-3: Using shorthand ‘+=’ operator to combine string

Another way of concatenating string data in bash is by using shorthand (+=) operator. Create a file named ‘concat3.sh’ and add the following code to check the use of shorthand operator. Here, the shorthand operator, ‘+=’ is used inside a ‘for’ loop to combine the elements of a list. At first, $food variable is initialized with an empty string. ‘for’ loop is used to read a list of four elements. Each value of the list will be combined with each other serially with a space in each iteration of the loop. The values are stored in the $foods variable. In the last statement, the value of $foods is printed.

Читайте также:  Linux lvm как удалить

Run the script by bash command.

#!/bin/bash
echo «Printing the list of foods»
#Initialize the variable before combine
foods = «»
#for loop for reading the list
for value in ‘Cake’ ‘ice-cream’ ‘Burger’ ‘Pizza’ ; do
#Combine the list values by using shorthand operator
foods+= » $value »
done
#Print the combined values
echo » $foods «

The following output will appear after executing the script.

Example-4: Combine using literal strings

The literal string variable can be used to combine with other string data. Create a file named, ‘concat4.sh‘ and add the following code. Here, $string variable is used to store string data and it is used as a literal string in `echo` command to print.

#!/bin/bash
#Declare a string variable
string = «Learn bash programming »
#Print the string variable as literal
echo » $ from the basics»

Run the script by bash command.

The following output will appear after executing the script.

Example-5: Combine strings with any particular character

Any particular character can be added inside to combine two or more strings data. This type of task is required when you need to add a separator among the string data. Each string value can be separated easily later if they are combined by a specific character. Create a file named, ‘concat5.sh‘ and add the following code. Here, three string data are taken as input at the beginning of the script. The values are assigned in three variables, those are $name, $address and $phone. These variables are combined with a character “:” and stored in $combine variable. This variable is redirected to a file named ‘customers.txt’ to append the values of $combine to the file. If the file does not exist then the file will be created and the value of $combine will be added at the beginning of the file. If the file exists the value of $combine will be added at the end of the file. If you want to extract the values from this data then you have to use the ‘:’ character to separate each part from the data.

#!/bin/bash
# Taking first input
echo -n «Enter customer’s name: »
read name
#Taking second input
echo -n «Enter customer’s address: »
read address
#Taking third input
echo -n «Enter customer’s phone: »
read phone
#Store the values by adding ‘:’ between two variables
combine = » $name : $address : $phone «

Читайте также:  Is sun solaris unix or linux

#Write the combined values into a file
echo » $combine » >> customers.txt

Run the script by bash command.

View the file, customers.txt to check the data is properly added to the file or not.

The following output will appear after executing the above commands.

Conclusion

String concatenation requires in the programming language to generate meaningful output. The output of the script needs to be formatted by combining data properly. All possible ways of combing strings in bash are tried to explain in this tutorial. Hope, the users will be benefited after practicing this tutorial and able to combine strings properly in the bash script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How To Bash Concatenate or Add Strings?

Bash provides string operations. We can use different operations like remove, find or concatenate strings in bash. In this tutorial we will look how to add or concatenate strings in Linux bash.

Put Variables Side By Side

The simplest and easy to understand way to concatenate string is writing the variables side by side. We will write the variables like $a$b . We do not need extra operater or function to use. In this example we will concatenate two variables named $a and $b into $c .

a="This is" b="poftut.com website" c=$a$b echo $c

Put Variables Side By Side

Double Quotes

Other useful alternative is using string variables inside strings which is defined with double quotes. We will put the variables name into double quotes with soe string. In this example we will put variable name $a into string Welcome

a=" to poftut" c="Welcome $a" echo $c

Double Quotes

Append Operator

Popular programming languages provides += compact add operator which is consist of plus and equal sign. We can add existing variable new string. This will add new string to the end of string variable. In this example variable named $a has all ready string value Welcome and we will add to poftut like below.

a="Welcome" a+=" to poftut" echo $a

Append Operator

Printf Function

printf is a function used to print and concatenate strings in bash. We can provide the string we want to print into a variable. We will use -v option with the variable name and the string we want to add. In this example we will use string variable $a and to poftut .

a="Welcome" printf -v c "$a to poftut" echo $c

Источник

Оцените статью
Adblock
detector