Premium lesson

Working With Strings in Bash

Scripting3 min readFeb 24, 2025

In programming, strings are one of the most commonly used data types.

In the context of Bash, string manipulation is crucial as many tasks involve processing text in files, environment variables, or command outputs.

Concatenating strings

In programming, concatenating strings refers to the process of linking together multiple strings into a single one.

It is mostly used to combine directory paths and filenames to dynamically create full paths, create formatted strings for logs or user feedback, generate URLs with query parameters or endpoints, and so on.

To concatenate multiple strings contained in variables, you can enclose the value of these variables in double quotes:

"$variable_A$variable_B"

馃挕 Reminder: To access the value of a variable in Bash, you must prepend it with a dollar sign $.

Example

Let's consider this script located in the ~/scripts directory, that outputs its own content:

#!/bin/bashdirectory="$HOME/scripts"file="reflect.sh"path="$directory/$file"
icon light bulb key

Unlock the CLI & Scripting with Bash module

Learn how to gain advanced control over the OS and automate complex routine tasks prone to manual errors with the CLI and Bash scripting.

You get immediate access to:

  • 34 focused lessons across the CLI and Bash
  • 4 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module