Bash 基础知识系列 #6：处理字符串操作
Bash 脚本
![](/data/attachment/album/202307/30/090030gvm6pgutlvzll4zg.jpg)


在大多数编程语言中，你都会找到字符串数据类型。字符串基本上是一组字符。


但 Bash Shell 有所不同。字符串没有单独的数据类型。这里一切都是变量。


但这并不意味着你不能像在 C 和其他编程语言中那样处理字符串。


在 Bash Shell 中可以查找子字符串、替换子字符串、连接字符串以及更多字符串操作。


在 Bash 基础知识系列的这一部分中，你将学习基本的字符串操作。


### 在 Bash 中获取字符串长度


让我们从最简单的选项开始。也就是获取字符串的长度。这很简单：



```
${#string}

```

让我们在示例中使用它。


![Example of getting string length in bash](/data/attachment/album/202307/30/090053lzt0jo8arresae6y.png)


正如你所看到的，第二个示例中有两个单词，但由于它用引号引起来，因此它被视为单个单词。连空格都算作一个字符。


### 在 Bash 中连接字符串


用技术术语来说是字符串 <ruby> 连接 <rt>  concatenation </rt></ruby>，这是 Bash 中最简单的字符串操作之一。


你只需像这样一个接一个地使用字符串变量：



```
str3=$str1$str2

```

还能比这更简单吗？我觉得不能。


让我们看一个例子。这是我的示例脚本，名为 `join.sh`：



```
#!/bin/bash

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

joined=$str1$str2

echo "The joined string is: $joined"

```

以下是该脚本的运行示例：


![Join two strings in bash](/data/attachment/album/202307/30/090053g3zoruhrh7w7r22o.png)


### 在 Bash 中提取子字符串


假设你有一个包含多个字符的大字符串，并且你想要提取其中的一部分。


要提取子字符串，需要指定主字符串、子字符串的起始位置和子字符串的长度，如下所示：



```
${string:$pos:$len}

```


> 
> ? 和数组一样，字符串中的定位也是从 0 开始。
> 
> 
> 


这是一个例子：


![Extracting substring in bash](/data/attachment/album/202307/30/090054rtxev2i5aezeo3yx.png)


即使你指定的子字符串长度大于字符串长度，它也只会到达字符串末尾。


### 替换 Bash 中的子字符串


假设你有一个大字符串，并且你想用另一个字符串替换其中的一部分。


在这种情况下，你可以使用这种语法：



```
${string/substr1/substr2}

```


> 
> ✋ 只有第一次出现的子字符串才会以这种方式替换。如果要替换所有出现的地方，请使用 `${string//substr1/substr2}`
> 
> 
> 


这是一个例子：


![Replace substring in bash](/data/attachment/album/202307/30/090054caii0ib0dladsuaw.png)


正如你在上面看到的，“good” 一词被替换为 “best”。我将替换的字符串保存到同一字符串中以更改原始字符串。



> 
> ? 如果未找到子字符串，则不会替换任何内容。它不会导致错误。
> 
> 
> 


### 在 Bash 中删除子字符串


我们来谈谈删除子字符串。假设你要删除字符串的一部分。在这种情况下，只需将子字符串提供给主字符串，如下所示：



```
${string/substring}

```


> 
> ✋ 通过这种方式，仅删除第一次出现的子字符串。如果要删除所有出现的内容，请使用 `${string//substr}`
> 
> 
> 


如果找到子字符串，则将从字符串中删除它。


让我们通过一个例子来看看。


![Delete substring in bash](/data/attachment/album/202307/30/090054vz4lpr501pycccta.png)


不用说，如果没有找到子字符串，则不会删除它。它不会导致错误。


### ?️ 练习时间


现在是你通过简单练习来实践字符串操作的时候了。


**练习 1**：声明一个字符串 “I am all wet”。现在通过用 “set” 替换单词 “wet” 来更改此字符串。


**练习 2**：创建一个字符串，以 `112-123-1234` 格式保存电话号码。现在，你必须删除所有 `-`。


这应该会给你一些在 Bash 中使用字符串的不错的练习。在下一章中，你将学习如何在 Bash 中使用 `if-else` 语句。敬请关注。


*（题图：MJ/aa73b2c9-6d2f-42e2-972d-94fab56d30cc）*




---


via: <https://itsfoss.com/bash-strings/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Chapter #6: Handling String Operations

In this chapter of the Bash Basics series, learn to perform various common string operations like extracting, replacing and deleting substrings.

In most programming languages, you'll find a string data type. A string is basically a group of characters.

Bash shell is different though. There is no separate data type for strings. Everything is a variable here.

But that doesn't mean that you cannot deal with strings in the same way you do in C and other programming languages.

Finding substrings, replacing substrings, joining strings and many more string operations are possible in Bash shell.

In this part of the Bash Basics Series, you'll learn the basic string manipulations.

## Get string length in bash

Let's start with the simplest option. Which is to get the length of a string. It's quite simple:

`${#string}`


Let's use it in an example.

![Example of getting string length in bash](https://itsfoss.com/content/images/2023/07/bash-string-length-example.png)

As you can see, the second example had two words in it but since it was in quotes, it was treated as a single word. Even the space is counted as a character.

## Join strings in bash

The technical term is concatenation of strings, one of the simplest possible string operations in bash.

You just have to use the string variables one after another like this:

`str3=$str1$str2`


Can it go any simpler than this? I don't think so.

Let's see it with an example. Here is my example script named `join.sh`

:

```
#!/bin/bash
read -p "Enter first string: " str1
read -p "Enter second string: " str2
joined=$str1$str2
echo "The joined string is: $joined"
```


Here's a sample run of this script:

![Join two strings in bash](https://itsfoss.com/content/images/2023/07/join-strings-bash.png)

## Extract substring in bash

Let's say you have a big string with several characters and you want to extract part of it.

To extract a substring, you need to specify the main string, the starting position of the substring and the length of the substring in the following manner:

`${string:$pos:$len}`


Here's an example:

![Extracting substring in bash](https://itsfoss.com/content/images/2023/07/extract-substring-bash.png)

Even if you specify the substring length greater than the string length, it will only go till the end of the string.

## Replace substring in bash

Let's say you have a big string and you want to replace part of it with another string.

In that case, you use this kind of syntax:

`${string/substr1/substr2}`


`${string//substr1/substr2}`

Here's an example:

![Replace substring in bash](https://itsfoss.com/content/images/2023/07/replace-substring-bash.png)

As you can see above, the word good was replaced with best. I saved the replaced string to the same string to change the original.

## Delete substring in bash

Let's talk about removing substrings. Let's say you want to remove part of a string. In that case, just provide the substring to the main string like this:

`${string/substring}`


`${string//substr}`

If the substring is found, it will be deleted from the string.

Let's see this with an example.

![Delete substring in bash](https://itsfoss.com/content/images/2023/07/bash-delete-substring.png)

This goes without saying that if the substring is not found, it is not deleted. It won't result in an error.

## 🏋️ Exercise time

It's time for you to practice string manipulation with simple exercises.

**Exercise 1**: Declare a string 'I am all wet'. Now change this string by replacing the word wet with set.

**Exercise 2**: Create a string that saves phone numbers in the following format `112-123-1234`

. Now, you have to delete all `-`

.

The answers can be discussed in this dedicated thread in the Community.

[Practice Exercise in Bash Basics Series #6: Handling String OperationsIf you are following the Bash Basics series on It’s FOSS, you can submit and discuss the answers to the exercise at the end of the chapter: Fellow experienced members are encouraged to provide their feedback to new members. Do note that there could be more than one answer to a given problem.](https://itsfoss.community/t/practice-exercise-in-bash-basics-series-6-handling-string-operations/10925)![](https://itsfoss.community/uploads/default/original/1X/f274f9749e3fd8b4d6fbae1cf90c5c186d2f699c.png)


![](https://itsfoss.community/uploads/default/original/1X/f274f9749e3fd8b4d6fbae1cf90c5c186d2f699c.png)

That should give you some decent practice with strings in bash. In the next chapter, you'll learn about [using if-else statements in bash](https://itsfoss.com/bash-if-else/).

[Bash Basics Series #7: If Else StatementIf this, then that else something else. Doesn’t make sense? It will after you learn about the if-else statements in bash shell scripting.](https://itsfoss.com/bash-if-else/)![](https://itsfoss.com/content/images/2023/07/bash-series-7-if-else.png)


![](https://itsfoss.com/content/images/2023/07/bash-series-7-if-else.png)

Stay tuned.