How to Extract Substrings Using ${} in Bash Scripts – TecAdmin

Channel: Linux
Abstract: " new_greeting="${greeting/John/Jane}" echo $new_greeting123greeting="Helloincluding extracting substrings from a given string. In this article

Bash (Bourne Again SHell) is a popular shell scripting language, especially in the Linux and Unix world. One of the most common tasks in shell scripting is manipulating strings, including extracting substrings from a given string. In this article, we’ll explore how to use the powerful ${} syntax in Bash scripts to cut or extract parts of a string. We’ll also demonstrate various examples to make you comfortable with this technique.

1. Getting Started with Bash String Manipulation

Before diving into extracting substrings, let’s briefly discuss the basics of string manipulation in Bash. Strings are simply sequences of characters enclosed in quotes. You can use single quotes (‘ ‘) or double quotes (」 「) to define a string, depending on whether you need variable expansion or not.

For example, consider the following string:

name="John Doe"1name="John Doe"

Here, we’ve assigned the string 「John Doe」 to the variable name. Now that we have a string to work with, let’s explore how to extract substrings.

2. Extracting Substrings Using ${}

In Bash, you can extract substrings using the {variable:offset:length} syntax, where:

  • variable is the name of the variable containing the string
  • offset is the position of the first character to be extracted (0-indexed)
  • length is the number of characters to extract (optional)

Here are some examples:

  • Extract the first name:
    first_name="${name:0:4}" echo $first_name12first_name="${name:0:4}"echo $first_name
  • Extract the last name:

    last_name="${name:5}" echo $last_name12last_name="${name:5}"echo $last_name


Notice that we didn’t specify the length in the second example, which means the substring extraction continues until the end of the string.

3. Using Negative Offset Values

You can also use negative offset values to extract substrings from the end of the string. The syntax is {variable: -offset:length}, and you must include the space before the negative sign. Here’s an example:

last_name="${name: -3}" echo $last_name12last_name="${name: -3}"echo $last_name

4. Finding and Replacing Substrings

Another powerful feature of Bash string manipulation is the ability to find and replace substrings. You can use the {variable/find/replace} syntax to achieve this. Here’s an example:

greeting="Hello, John Doe!" new_greeting="${greeting/John/Jane}" echo $new_greeting123greeting="Hello, John Doe!"new_greeting="${greeting/John/Jane}"echo $new_greeting

Hello, Jane Doe!

In this article, we’ve explored how to extract substrings from strings using the ${} syntax in Bash scripts. This powerful feature allows you to manipulate strings in various ways, including extracting, finding, and replacing substrings. With these techniques in hand, you’ll be well-equipped to handle a wide range of string manipulation tasks in your Bash scripts.

Now that you’ve mastered string manipulation using the ${} syntax, go ahead and apply this knowledge to enhance your shell scripting skills!

Ref From: tecadmin

Related articles