From e109ab368e6a74f8442c3c1a2fc0819fb7713886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AD=E5=BC=80=E7=AE=B1?= Date: Sat, 22 Jul 2023 21:45:56 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=89=8B=E5=8A=A8=E9=80=89=E9=A2=98][tech]:?= =?UTF-8?q?=2020230717.0=20=E2=AD=90=EF=B8=8F=E2=AD=90=EF=B8=8F=20Bash=20B?= =?UTF-8?q?asics=20Series=206=20Handling=20String=20Operations.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Bash Basics Series 6 Handling String Operations.md | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 sources/tech/20230717.0 ⭐️⭐️ Bash Basics Series 6 Handling String Operations.md diff --git a/sources/tech/20230717.0 ⭐️⭐️ Bash Basics Series 6 Handling String Operations.md b/sources/tech/20230717.0 ⭐️⭐️ Bash Basics Series 6 Handling String Operations.md new file mode 100644 index 0000000000..91191cde9d --- /dev/null +++ b/sources/tech/20230717.0 ⭐️⭐️ Bash Basics Series 6 Handling String Operations.md @@ -0,0 +1,149 @@ +[#]: subject: "Bash Basics Series #6: Handling String Operations" +[#]: via: "https://itsfoss.com/bash-strings/" +[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/" +[#]: collector: "lkxed" +[#]: translator: " " +[#]: reviewer: " " +[#]: publisher: " " +[#]: url: " " + +Bash Basics Series #6: Handling String Operations +====== + +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][1] + +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][2] + +### 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} +``` + +> 💡Like arrays, positioning in strings also start at 0. + +Here's an example: + +![Extracting substring in bash][3] + +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} +``` + +> ✋ Only the first occurrence of a substring is replaced this way. If you want to replace all occurrences, use`${string//substr1/substr2}` + +Here's an example: + +![Replace substring in bash][4] + +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. + +> 💡 If the substring is not found, nothing is replaced. It won't result in an error. + +### 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} +``` + +> ✋ Only the first occurrence of a substring is deleted this way. If you want to delete all occurrences, use`${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][5] + +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 `-`. + +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. Stay tuned. + +-------------------------------------------------------------------------------- + +via: https://itsfoss.com/bash-strings/ + +作者:[Abhishek Prakash][a] +选题:[lkxed][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://itsfoss.com/author/abhishek/ +[b]: https://github.com/lkxed/ +[1]: https://itsfoss.com/content/images/2023/07/bash-string-length-example.png +[2]: https://itsfoss.com/content/images/2023/07/join-strings-bash.png +[3]: https://itsfoss.com/content/images/2023/07/extract-substring-bash.png +[4]: https://itsfoss.com/content/images/2023/07/replace-substring-bash.png +[5]: https://itsfoss.com/content/images/2023/07/bash-delete-substring.png