Write variables side by side for Bash Concatenate String
This is the easiest method to perform concatenation.
Example: Let’s take two strings (i.e., “welcome” and “to w3wiki”), and we want to return a new string which is the combination of the given two strings.
We can use `str3 = “$str1$str2″`
Code:
#!/bin/bash
# Script to Concatenate Strings
# Declaration of first String
str1="Welcome"
# Declaration of Second String
str2=" to w3wiki."
# Combining first and second string
str3="$str1$str2"
# print the concatenated string
echo $str3
Output:
Here we first created the script name “side.sh” using Vim editor, then made our script executable using `chmod +x` command and finally run our script.
This method directly concatenates two strings by placing them adjacent to each other, forming the desired output.
Bash Concatenate String
Bash scripting, a powerful and versatile tool in the Linux world, allows users to automate tasks and streamline processes. String manipulation is a fundamental aspect of scripting, and one common operation is concatenating strings. Concatenation involves combining multiple strings into a single string, facilitating the creation of dynamic and customized outputs in scripts. In this article, we’ll delve into the intricacies of string concatenation in Bash, exploring various methods and providing illustrative examples.
Table of Content
- 1. Write variables side by side for Bash Concatenate String
- 2. Using Double Quotes for Concatenate String
- 3. Using printf function for Concatenate String
- 4. Using Literal String for Concatenate String
- 5. Using Loop for Concatenate String
- 6. Using Any Character for Concatenate String