BASH Programming

How To Remove Last n Characters From a String in Bash

Bash is a popular shell scripting language that is widely used in Linux and Unix-based operating systems. One of the common tasks in Bash scripting is to manipulate strings. Sometimes, it becomes necessary to remove the last n characters from a string. This article will go through several Bash methods for removing the final n characters from a string.

How To Remove Last n Characters From a String in Bash

In Bash, trimming trailing whitespace from user inputs or removing the final n characters from a string can be used to tidy up file names with undesirable extensions:

Method 1: Using cut Command

The cut command in Bash is used to extract sections from each line of a file. It can also be used to extract a specific range of characters from a string. To remove the last n characters from a string, we can use the cut command with the -c option, and here is the syntax:

echo "string" | cut -c -n

Here the string is the actual string from which we want to remove the last n characters, and n is the number of characters we want to remove, below is the example that uses the above syntax:

#!/bin/bash

string="Hello Linux"

echo "$string" | cut -c -5

In the above example, we have used the cut command to remove the last 6 characters from the string “Hello Linux” and the output is “Hello”.

Graphical user interface, text Description automatically generated

Method 2: Using sed Command

Sed is a powerful stream editor that can be used to perform various text transformations on a file or a stream of input. To remove the last n characters from a string using sed, we can use the following command syntax:

echo "string" | sed 's/.\{n\}$//'

Here, n is the number of characters we want to remove from the end of the string, and below is an example that uses the sed command:

#!/bin/bash

string="Hello Linux"

echo "$string" | sed 's/.\{6\}$//'

In the above example, we have used the sed command to remove the last 6 characters from the string “Hello Linux” and the output is “Hello”.

Graphical user interface, text Description automatically generated

Method 3: Using Parameter Expansion

Parameter expansion is a feature in Bash that allows us to manipulate the value of a variable. To remove the last n characters from a string using parameter expansion, we can use the following syntax:

${string::-n}

Here, the string variable is containing the actual string from which we want to remove the last n characters, and n is the number of characters we want to remove.

#!/bin/bash

string="Hello Linux"

echo ${string::-6}

In the above example, we have used parameter expansion to remove the last 4 characters from the string “Hello Linux” and the output is “Hello”.

Graphical user interface, text Description automatically generated

Conclusion

To remove the last n characters from a string in bash, the cut command, sed command, and parameter expansion are the three ways. These methods are easy to use and can be helpful in various Bash scripting tasks. By using these methods, we can easily manipulate strings and perform text transformations in Bash.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.