BASH Programming

Bash | Truncate String Variable

One of the essential features of bash scripting is the ability to manipulate strings using various methods, including truncating a string variable. In this article, we will explore what a truncate string variable is and how to use it in bash with examples.

What is Truncate String Variable – Bash

A truncate string variable is a technique used in programming to shorten a string to a specific length. In bash scripting, the truncate command is used to accomplish this. By using this command, you can quickly and easily trim down the size of a string variable to your desired length.

In bash, there are several ways to truncate a string, including by character count, specified length, or from the beginning of the string. The syntax for truncating a string variable in bash is as follows:

${<string-name>:<starting-position>:<length>}

 

How to Truncate String Variable in Bash

Suppose we have a string variable that contains a sentence, and we want to truncate it by the first ten characters then below is the bash example code that will help you in this regard:

#!/bin/bash
myString="Hello and welcome to LinuxHint.com"
echo "Original String:" $myString
truncatedString=${myString:0:10}
echo "Truncated String:" $truncatedString

 

Here, we are using the ${string:0:10} command to truncate the string variable from position 0 to 10 characters. The output of this command is “Hello and” which is the first ten characters of the string in short it is leaving the first 10 characters of the string and removing all the remaining, so this method is  useful if you want to cut characters from the end:

Now for instance if you want to cut the string variable from the end then below is the bash script for it:

#!/bin/bash
myString="Hello and welcome to LinuxHint.com"
echo "Original String:" $myString
truncatedString=${myString:0:-10}
echo "Truncated String:" $truncatedString

 

In this case, the position value is 0, which means the substring will start at the beginning of the string. The length value is -10, which means that the substring will include all characters in myString except for the last 10. The – before the 10 means that the length value is negative, which is a special case in bash that means “count from the end of the string.

The above examples are useful if you want to make a boundary for truncating a string variable using the starting position, however if you just want to remove some part of string then, below is the example bash script:

#!/bin/bash
myString="Hello and welcome to LinuxHint.com"
echo "Original String:" $myString
truncatedString=${myString: -10}
echo "Truncated String:" $truncatedString

 

Here, we are using the ${string: -10} command to truncate the string variable from the last 10 characters of the string. The output of this command is “unixHint.com,” which is the last 10 characters of the string.

So, if you want to remove the characters from the end then just remove the negative sign and give the number of the characters you want to remove from the beginning like done in this script:

#!/bin/bash
myString="Hello and welcome to LinuxHint.com"
echo "Original String:" $myString
truncatedString=${myString: 10}
echo "Truncated String:" $truncatedString

 

The script then uses the ${string:position} syntax to create a new variable truncatedString which is a substring of myString. In this case, the position value is 10, which means the substring will start at the 11th character of the string (since bash is 0-indexed). Since no length value is specified, the substring will include all characters from the specified position to the end of the string.

Conclusion

Truncating string variables is a common task in bash scripting as it provides the ability to easily extract a portion of a string based on its position or length is a powerful tool that can be used to manipulate and transform data in a variety of ways. This article covered what a truncated string variable is and how to use it in bash with the help of some examples.

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.