Linux bash concat strings

How to Concatenate Strings in Bash?

In bash scripting, we can add or join two or more strings which are called string concatenation. For example, two strings having “Linux ” and “Ubuntu” can be concatenated to form a single string as “Linux Ubuntu” or “Ubuntu Linux”.

The bash script does not have any built-in function to perform string concatenation, but various methods can be utilized to perform string concatenation.

This article will discuss the possible ways to concatenate the string with the below timeline:

  • How to Concatenate Bash Strings?
    • Example 1: Concatenate Two Bash Strings
    • Example 2: Concatenate Strings Taking User Input
    • Example 3: Concatenate Strings Using the Curly Braces <>
    • Example 4: Concatenate Strings Using the +=
    • Example 5: Concatenate Multiple Bash Strings
    • Example 6: Concatenate Strings Using for Loop

    How to Concatenate Bash Strings?

    There are several ways to concatenate strings; let’s understand the concatenating strings methods with the help of examples:

    Example 1: Concatenate Two Bash Strings

    To concatenate two strings, we can simply write them together. For instance, two strings variables are created, “Str1” and “Str2”, which will be concatenated in the below bash script:

    Note: The strings can be concatenated with (as done in the below script) or without space to add blank space between two strings.

    #!/bin/bash Str1="Linux," Str2="Ubuntu" echo "$Str1 $Str2"

    Run the bash script file using the below command:

    The output shows that both strings are concatenated.

    Example 2: Concatenate Strings Taking User Input

    To concatenate two strings with the user input, we can use the below script:

    #!/bin/bash read -p "Enter your first string: " Str1 read -p "Enter your second string: " Str2 echo "Your string is: $Str1 $Str2"

    Let’s run the script, with the below bash command:

    The output shows two strings are entered by the user “Linux” and “Ubuntu”, which is concatenated as “Linux Ubuntu”,

    Example 3: Concatenate Strings Using the Curly Braces <>

    The curly braces <>, the format is used to concatenate the strings. To concatenate two strings variables Str1 “Linux, ” and Str2 “Ubuntu”, the below script is used:

    #!/bin/bash Str1="Linux, " Str2="Ubuntu" echo "$$"

    To check the output of the above script, use the following command:

    The output shows that both strings are concatenated as “Linux, Ubuntu”.

    Example 4: Concatenate Strings Using the +=

    You can concatenate two strings using the “+=” operator. For instance, we have used a single variable Str1 “Linux, ” which can be concatenated with another string “Ubuntu”, using the += operator with the below bash script:

    #!/bin/bash Str1="Linux, " Str1+="Ubuntu" echo "$Str1"

    To run the script, use this command:

    Two strings are concatenated, and the output can be seen as “Linux, Ubuntu”.

    Example 5: Concatenate Multiple Bash Strings

    Several strings can be concatenated by using the names together. For instance, to concatenate four strings, the below script can be utilized:

    #!/bin/bash Str1="Linux, " Str2="Debian, " Str3="RHEL, " Str4="Fedora" echo "$Str1$Str2$Str3$Str4"

    To run the above bash script, use the following command:

    The four strings are concatenated, as seen in the output.

    Example 6: Concatenate Strings Using for Loop

    We can concatenate the string using the for loop. To concatenate a number from a loop with a string, use this script:

    #!/bin/bash Numbers="1 3 5" Result="" for Num in $Numbers do Result+="The Number is: $. " done echo $Result

    Let’s understand the script line by line:

    • Numbers: We declare a Variable having numbers, and every number will be concatenated with the string.
    • Result: Will store the concatenated string.
    • for: The for loop will run every time, and a number will be concatenated with the string.
    • Result+= : It adds the number with the string.

    Let’s run the above shell script using the following command:

    The number is: ” text is concatenated with an element of Numbers when the for loop runs.

    Additional Tip: Concatenate Two Numbers

    Two numbers can also be concatenated; when we concatenate two numbers, it does not add the number rather, it just appends the number to the first number. For instance, to concatenate two numbers, “Num1” and “Num2”, the below shell script can be used:

    #!/bin/bash #!/bin/bash Num1=15 Num2=12 Num3="$Num1$Num2" echo "$Num3"

    To execute the script the following bash command is utilized:

    The output shows the two numbers are concatenated (12 is appended with 15).

    Conclusion

    Several methods are used to concatenate the strings, such as writing the strings together ($Str1$Str2), using curly braces ($$ ), and using the += operator (Str1+=”$Str2”). Moreover, we can concatenate the string using the for loop. This post has briefly explained the various methods to concatenate strings in bash.

    Источник

    Bash Concatenate Strings

    [#what-is-concatenation]What is Concatenation?[#what-is-concatenation]

    Concatenation is the process of joining two strings together into one result. Some common use cases include creating a string containing multiple variables, and formatting messages containing user-defined inputs.

    [#basic-concatenation]Basic Concatenation[#basic-concatenation]

    In Bash, when variables or strings are written one after another, they automatically concatenate. In the following examples, the code will echo the same output to the terminal.

     $ X="String" $ Y="Concatenation!" $ echo "$$" # Output StringConcatenation!

    Concatenating a variable and a string:

     $ X="String" $ echo "$Concatenation!" # Output StringConcatenation!

    The curly braces are used to “protect” the names of the X and Y variables from other characters in the string. They make it clear to Bash that you are using a variable with a specific name. Use double quotes for strings containing variable names — this tells Bash to interpolate, or substitute in, the values of the variables.

    [#mult-variable-types]Concatenation with multiple variable types[#mult-variable-types]

    Bash infers the type of variables depending on how they are used. So, this means we can use integer or boolean variables in string concatenation as well.

    Concatenating an integer and a string:

     $ X="The number four: " $ Y=4 $ echo "$X$Y" # Output The number four: 4

    Concatenating multiple numbers:

     $ X=314 $ Y=159 $ echo "$X$Y" # Output 314159

    [#using-addition-assignment-operator]Concatenation with += operator[#using-addition-assignment-operator]

    You can use the += operator to append to the end of a string variable.

     $ X="Pi is the number " $ X+=3.14159 $ X+= " and it is very handy." $ echo "$X" # Output Pi is the number 3.14159 and it is very handy.

    Источник

    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.

    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.

    Источник

    Читайте также:  Linux mint восстановить удаленный файл
Оцените статью
Adblock
detector