HomeLinuxBash Fundamentals Sequence #6: Dealing with String Operations

Bash Fundamentals Sequence #6: Dealing with String Operations


In most programming languages, you may discover a string knowledge kind. A string is mainly a gaggle of characters.

Bash shell is totally different although. There is no such thing as a separate knowledge kind for strings. All the pieces is a variable right here.

However that does not imply that you just can’t take care of strings in the identical approach you do in C and different programming languages.

Discovering substrings, changing substrings, becoming a member of strings and lots of extra string operations are potential in Bash shell.

On this a part of the Bash Fundamentals Sequence, you may be taught the fundamental string manipulations.

Get string size in bash

Let’s begin with the best possibility. Which is to get the size of a string. It is fairly easy:

${#string}

Let’s use it in an instance.

Example of getting string length in bash

As you may see, the second instance had two phrases in it however because it was in commas, it was handled as a single phrase. Even the area is counted as a personality.

Be a part of strings in bash

The technical time period is concatenation of strings and this is without doubt one of the easiest potential string operations in bash.

You simply have to make use of the string variables one after one other like this:

str3=$str1$str2

Can it go any easier than this? I do not suppose so.

Let’s examine it with an instance. Right here is my instance script named be a part of.sh:

#!/bin/bash

learn -p "Enter first string: " str1
learn -p "Enter second string: " str2

joined=$str1$str2

echo "The joined string is: $joined"

Here is a pattern run of this script:

Join two strings in bash

To illustrate you will have a giant string with a number of characters and also you need to extract a part of it.

To extract a substring, it’s worthwhile to specify the principle string, the beginning place of the substring and the size of the substring within the following method:

${string:$pos:$len}

💡

Like arrays, positioning in strings additionally begin at 0.

Here is an instance:

Extracting substring in bash

Even for those who specify the substring size higher than the string size, it can solely go until the tip of the string.

Exchange substring in bash

To illustrate you will have a giant string and also you need to exchange a part of it with one other string.

In that case, you employ this sort of syntax:

${string/substr1/substr2}

Solely the primary incidence of a substring is changed this manner. If you wish to exchange all occurrences, use ${string//substr1/substr2}

Here is an instance:

Replace substring in bash

As you may see above, the phrase good was changed with greatest. I saved the changed string to the identical string to alter the unique.

💡

If the substring shouldn’t be discovered, nothing is changed. It will not lead to an error.

Delete substring in bash

Let’s speak about eradicating substrings. To illustrate you need to take away a part of a string. In that case, simply present the substring to the principle string like this:

${string/substring}

Solely the primary incidence of a substring is deleted this manner. If you wish to delete all occurrences, use ${string//substr}

If the substring is discovered, will probably be deleted from the string.

Let’s examine this with an instance.

Delete substring in bash

This goes with out saying that if the substring shouldn’t be discovered, it’s not deleted. It will not lead to an error.

🏋️ Train time

It is time so that you can apply string manipulation with easy workouts.

Train 1: Declare a string ‘I’m all moist’. Now change this string by changing the phrase moist with set.

Train 2: Create a string that saves telephone numbers within the following format 112-123-1234. Now, you must delete all -.

That ought to provide you with some first rate apply with strings in bash. Within the subsequent chapter, you may find out about utilizing if-else statements in bash. Keep tuned.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments