Connect with us

Tips and Guides

Optimizing String Concatenation in Bash for Efficient Shell Scripting

Published

on

Optimizing String Concatenation in Bash for Efficient Shell Scripting

In the context of the Linux command line, “Bash” can refer to two separate, but related activities:

  • Issuing standalone commands
  • Issuing commands as part of a script in a file

Both of the above are examples of using bash. It’s important to note, however, that there are other shells than Bash. For example, there is “sh (Bourne Shell)”, zsh, fish, and more. Bash is the most common shell and you can find out which one you’re using by typing the following command:

echo $SHELL

This gives the following output:

Linux Determine Shell
Linux Determine Shell

In the above example, the output /bib/bash refers to the bash shell. Below, I will show you all the various ways of concatenating strings in bash, along with a few real-world examples of how administrators use string concatenation to improve their daily workflows, along with best practices and how to avoid mistakes.

Let’s get started!

How to Concatenate Strings in Bash

Here are all the different ways you can concatenate strings in bash. In these examples, I’m assuming that you’re using bash as a scripting language, and these examples are the contents of a file that we will execute.

Placing Adjacent Strings Next to Each Other

The easiest way to concatenate strings in Bash is to simply place them next to each other:

Advertisement
str1="Hello, "
str2="World!"
greeting="$str1$str2"
echo $greeting # Outputs: Hello, World!

In the above example, str1 and str2 are two strings. We can create a third string simply by placing the two variables next. As you can see, the words “Hello, ” and “World!” have been concatenated into a third string. Easy, right?

Using Curly Braces to Disambiguate Variables

In the above example, the final concatenation consisted of nothing but the two strings. However, this can get confusing if you also need to output ordinary text next to the variable names. Punctuation marks, for example, are an excellent use case. Consider the following piece of code:

username="alice"
echo "Welcome, ${username}!" # Outputs: Welcome, alice!

Above, we use the “username” variable to output “alice”. However, note that there’s no space between “alice” and the subsequent exclamation mark. How does the system know where the variable name begins and where it ends? With normal programming languages, you need to break the string into various parts, which can get annoying to read. However, in bash, we just surround the variable name with curly braces to indicate the start and stop of the variable. So the system doesn’t get confused between variable names and the actual text.

Assigning Strings Directly with Concatenation

In the above two examples, I’ve used the “echo” command to concatenate and display the strings. However, we can also use concatenation to assign the strings directly without outputting anything. For example:

file_prefix="photo"
file_suffix=".jpg"
file_name="${file_prefix}_$(date +%Y%m%d)${file_suffix}"
# This might output: photo_20231106.jpg

In the above example, I’m appending three variables together, and throwing in an underscore (_) for good measure. Here I am constructing a file name by adding:

Advertisement
  1. The file prefix
  2. And underscore (_)
  3. The date
  4. The file suffix

I’m assigning this file name to another variable directly.

String Concatenation via Command Substitution

This is an interesting use case. Let’s say you want to create a script that references a specific file with a given date and want to prefix it with the directory so you can get the full location. You could, of course, do this easily using the second example. But if you want cleaner code, you might consider something like this:

dir="/var/log"
filename="$(date +%Y%m%d)_report.log"
full_path="${dir}/${filename}"
# This might output: /var/log/20230328_report.log

In the above example, the final string “full_path” is simply assigned “${dir}/${filename}”. The “filename” variable is itself a product of string concatenation containing the date. This method allows you to reuse “filename” several times, with the knowledge that it always represents the current date. This makes it easier to read and maintain code.

Using “+=” to Concatenate Strings

Bash follows in the footsteps of the C programming language, and allows you to concatenate strings using the “+=” operator. Here’s an example of how it works:

str="Hello"
str+=" World"
echo $str # Outputs: Hello World

The above syntax evolved from C, and the two following types of statements were similar:

a = a + b
a += b

The latter had advantages in terms of efficiency for older languages, and in some cases made the code easier to read. Bash uses a similar notation to append strings to one another. Here’s the output:

Advertisement
String Concatenation in Linux Bash
String Concatenation in Linux Bash

So you can see that the above method works for strings as well as for numbers.

String Concatenation with printf

Bash’s printf command is a powerful tool for concatenating strings in a way that could be easier to read because it’s similar to how we learned to output strings in C and is also easier to write for some people.

For example, here’s a small script to concatenate two strings:

str1="Hello, "
str2="World!"
printf "%s%s\n" "$str1" "$str2"
# Outputs: Hello, World!

In the above piece of code, the first argument to printf “%s%s” tells the command to expect two strings and to display them in the order that they arrive. Then we pass the arguments to the command and it displays “Hello, World” as expected. You can even use the above method to mix in multiple string literals and variables. For example:

name="Alice"
date=$(date +%Y-%m-%d)
printf "Meeting with %s on %s\n" "$name" "$date"
# Outputs: Meeting with Alice on 2023-11-08

Here we are concatenating many different string literals with variables sprinkled in.

Using HEREDOC to Concatenate Strings

This is my favorite method of concatenating long strings spanning multiple lines in a specific format. I find it particularly useful if you’re outputting code like JavaScript or PHP because you can use all kinds of special characters without worrying that the tool will interpret them in special ways.

Advertisement

It’s easy to show how it works with an example:

cat <<#eof> output.txt
This is the first line.
This is the second line with a variable: $variable.
EOF<#/eof>

In the above example, if you have a $variable with the content “world”, then it will save the following text to a file “output.txt”:

This is the first line. 
This is the second line with a variable: $variable.

The heredoc block starts with "<<#EOF" and ends as soon as the processor encounters the characters “EOF” by themselves on a new line. It’s important to note that there shouldn’t be any spaces both before and after the final EOF. As long as you stick to these rules, you should be able to use EOF particularly well for storing code like HTML syntax that you would normally have to constantly escape, thanks to the presence of single and double quotes everywhere.

String Concatenation Best Practices

Here are some tips for ensuring that you can smoothly concatenate strings in Bash.

Use curly braces for variables. They help you separate the variables from the text, particularly punctuation marks!

Advertisement

Remember to escape your characters if they are special symbols like the dollar sign ($) backslashes (\) or quotation marks (“). You can escape them by adding a backslash (\) before them. Like this “\$”.

Use HEREDOC for multiple line strings. Readability is important, especially for long strings. Ensure that you use the HEREDOC standard if you have a lot of text.

Conclusion

As you can see, Bash provides you with a dozen ways to concatenate strings. Each of them has its uses depending on whether you’re going for simplicity, readability, or functionality. In addition, some techniques like command substitution can spawn subshells, so there’s even a performance consideration when you’re concatenating large numbers of strings either in loops or recursively.

I hope you found some of the examples here useful. Good luck with concatenating your strings!

Advertisement

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.