Script bash linux string

Bash Beginner Series #6: String Operations in Bash

Let’s pull some strings and learn to handle strings in bash scripts.

Let’s manipulate some strings!

If you are familiar with variables in bash, you already know that there are no separate data types for string, int etc. Everything is a variable.

But this doesn’t mean that you don’t have string manipulation functions.

In the previous chapter, you learned arithmetic operators in Bash. In this chapter, you will learn how to manipulate strings using a variety of string operations. You will learn how to get the length of a string, concatenate strings, extract substrings, replace substrings, and much more!

Get string length

Let’s start with getting the length of a string in bash.

A string is nothing but a sequence (array) of characters. Let’s create a string named distro and initialize its value to “Ubuntu”.

Now to get the length of the distro string, you just have to add # before the variable name. You can use the following echo statement:

Do note that echo command is for printing the value. is what gives the length of string.

Concatenating two strings

You can append a string to the end of another string; this process is called string concatenation.

To demonstrate, let’s first create two strings str1 andstr2 as follows:

Now you can join both strings and assign the result to a new string named str3 as follows:

Читайте также:  Альт линукс нет звука

It cannot be simpler than this, can it?

Concatenate strings in bash shell

Finding substrings

You can find the position (index) of a specific letter or word in a string. To demonstrate, let’s first create a string named str as follows:

Now you can get the specific position (index) of the substring cool. To accomplish that, use the expr command:

[email protected]:~/scripts$ word="Cool" [email protected]:~/scripts$ expr index "$str" "$word" 9

The result 9 is the index where the word “Cool” starts in the str string.

I am deliberately avoiding using conditional statements such as if, else because in this bash beginner series, conditional statements will be covered later.

Extracting substrings

You can also extract substrings from a string; that is to say, you can extract a letter, a word, or a few words from a string.

To demonstrate, let’s first create a string named foss as follows:

foss="Fedora is a free operating system"

Now let’s say you want to extract the first word “Fedora” in the foss string. You need to specify the starting position (index) of the desired substring and the number of characters you need to extract.

Therefore, to extract the substring “Fedora”, you will use 0 as the starting position and you will extract 6 characters from the starting position:

Notice that the first position in a string is zero just like the case with arrays in bash. You may also only specify the starting position of a substring and omit the number of characters. In this case, everything from the starting position to the end of the string will be extracted.

For example, to extract the substring “free operating system” from the foss string; we only need to specify the starting position 12:

[email protected]:~/scripts$ echo $ free operating system

Replacing substrings

You can also replace a substring with another substring; for example, you can replace “Fedora” with “Ubuntu” in the foss string as follows:

[email protected]:~/scripts$ echo $ Ubuntu is a free operating system

Let’s do another example, let’s replace the substring “free” with “popular”:

[email protected]:~/scripts$ echo $ Fedora is a popular operating system

Since you are just printing the value with echo command, the original string is not reallt altered.

Читайте также:  Linux полные имена файлов

Deleting substrings

You can also remove substrings. To demonstrate, let’s first create a string named fact as follows:

You can now remove the substring “big” from the string fact:

[email protected]:~/scripts$ echo $ Sun is a star

Let’s create another string named cell:

Now let’s say you want to remove all the dashes from the cell string; the following statement will only remove the first dash occurrence in the cell string:

To remove all dash occurrences from the cell string, you have to use double forward slashes as follows:

Notice that you are using echo statements and so the cell string is intact and not modified; you are just displaying the desired result!

To modify the string, you need to assign the result back to the string as follows:

[email protected]:~/scripts$ echo $cell 112-358-1321 [email protected]:~/scripts$ cell=$ [email protected]:~/scripts$ echo $cell 1123581321

Converting upper and lower-case letters in string

You can also convert a string to lowercase letter or uppercase letters. Let’s first create two string named legend and actor:

legend="john nash" actor="JULIA ROBERTS"

You can convert all the letters in the legend string to uppercase:

You can also convert all the letters in the actor string to lowercase:

[email protected]:~/scripts$ echo $ julia roberts

You can also convert only the first character of the legend string to uppercase as follows:

Likewise, you can convert only the first character of the actor string to lowercase as follows:

[email protected]:~/scripts$ echo $ jULIA ROBERTS

You can also change certain characters in a string to uppercase or lowercase; for example, you can change the letters j and n to uppercase in the legend string as follows:

Читайте также:  Linux перезагрузка при зависании

Awesome! This brings us to the end of this tutorial in the bash beginner series. Download the below PDF and practice what you just learned.

I hope you have enjoyed doing string manipulation in bash and check the next chapter as you will learn how to add decision-making skills to your bash scripts!

Источник

Оцените статью
Adblock
detector