# String Manipulation 字串處理

##### 字串長度

```shell
my_string="abhishek"
echo "length is ${#my_string}"
```

Using expr

```shell
str="my string"
length=$(expr length "$str")
echo "Length of my string is $length"
```

Using awk

```shell
echo "my string" | awk '{print length}'
```

Using wc

```shell
str="my string"
length=$(echo -n "my string" | wc -m)
echo "Length of my string is $length"
```

##### 字串比對

Using wildcards

```shell
#!/bin/bash

STR='GNU/Linux is an operating system'
SUB='Linux'
if [[ "$STR" == *"$SUB"* ]]; then
  echo "It's there."
fi
```

Using case

```shell
#!/bin/bash

STR='GNU/Linux is an operating system'
SUB='Linux'

case $STR in

  *"$SUB"*)
    echo -n "It's there."
    ;;
esac
```

Using Regex

```shell
#!/bin/bash

STR='GNU/Linux is an operating system'
SUB='Linux'

if [[ "$STR" =~ .*"$SUB".* ]]; then
  echo "It's there."
fi
```

Using Grep

```shell
#!/bin/bash

STR='GNU/Linux is an operating system'
SUB='Linux'

if grep -q "$SUB" <<< "$STR"; then
  echo "It's there"
fi
```

##### 字串合併

```shell
str1="hand"
str2="book"
str3=$str1$str2
```

```bash
STR="Hello"
STR+=", World"
echo $STR
```

##### 字元提取

```shell
foss="Fedora is a free operating system"
echo ${foss:0:6}
```

```shell
_admin_ip="202.54.1.33|MUM_VPN_GATEWAY 23.1.2.3|DEL_VPN_GATEWAY 13.1.2.3|SG_VPN_GATEWAY"
for e in $_admin_ip
do
   ufw allow from "${e%%|*}" to any port 22 proto tcp comment 'Open SSH port for ${e##*|}'
done
```

##### 字元取代

```shell
foss="Fedora is a free operating system"
echo ${foss/Fedora/Ubuntu}
```

##### Summary: String Manipulation and Expanding Variables

<table border="1" id="bkmrk-%24%7Bparameter%3A-default" style="border-collapse: collapse; width: 100%;"><tbody><tr><td style="width: 50%;">`${parameter:-defaultValue}`</td><td style="width: 50%;">Get default shell variables value</td></tr><tr><td style="width: 50%;">`${parameter:=defaultValue}`</td><td style="width: 50%;">Set default shell variables value</td></tr><tr><td style="width: 50%;">`${parameter:?"Error Message"}`</td><td style="width: 50%;">Display an error message if parameter is not set</td></tr><tr><td style="width: 50%;">`${#var}`</td><td style="width: 50%;">Find the length of the string</td></tr><tr><td style="width: 50%;">`${var%pattern}`</td><td style="width: 50%;">Remove from shortest rear (end) pattern</td></tr><tr><td style="width: 50%;">`${var%%pattern}`</td><td style="width: 50%;">Remove from longest rear (end) pattern</td></tr><tr><td style="width: 50%;">`${var:num1:num2}`</td><td style="width: 50%;">Substring</td></tr><tr><td style="width: 50%;">`${var#pattern}`</td><td style="width: 50%;">Remove from shortest front pattern</td></tr><tr><td style="width: 50%;">`${var##pattern}`</td><td style="width: 50%;">Remove from longest front pattern</td></tr><tr><td style="width: 50%;">`${var/pattern/string}`</td><td style="width: 50%;">Find and replace (only replace first occurrence)</td></tr><tr><td style="width: 50%;">`${var//pattern/string}`

echo "${PATH//:/$'\\n'}"

</td><td style="width: 50%;">Find and replace all occurrences</td></tr><tr><td style="width: 50%;">`${!prefix*}`</td><td style="width: 50%;">Expands to the names of variables whose names begin with prefix.</td></tr><tr><td style="width: 50%;">`${var,}`

`${var,pattern}`

</td><td style="width: 50%;">Convert first character to lowercase.</td></tr><tr><td style="width: 50%;">`${var,,}`

`${var,,pattern}`

</td><td style="width: 50%;">Convert all characters to lowercase.</td></tr><tr><td style="width: 50%;">`${var^}`

`${var^pattern}`

</td><td style="width: 50%;">Convert first character to uppercase.</td></tr><tr><td style="width: 50%;">`${var^^}`

`${var^^pattern}`

</td><td style="width: 50%;">Convert all character to uppercase.</td></tr></tbody></table>

##### Cheatsheet: String Manipulation

[![bash_string.jpeg](http://www.osslab.tw/uploads/images/gallery/2021-01/scaled-1680-/bash_string.jpeg)](http://www.osslab.tw/uploads/images/gallery/2021-01/bash_string.jpeg)

##### String To Integer

```shell
# $((string))
sum=$((3+6))
echo $sum

a=11
b=3
c=$(($a+$b))
echo $c
```

Alternate method: expr

> Note that it is not a “native” Bash procedure, as you need to have `coreutils` installed (by default on Ubuntu) as a separate package.

```shell
a=5; b=3; c=2; d=1
expr $a + $b \* $c - $d
```

##### 去除變數的末端字元 .XXX

```bash
# Operator "#" means "delete from the left, to the first case of what follows."
x="This is my test string."
echo ${x#* }

is my test string.

# Operator "##" means "delete from the left, to the last case of what follows."
x="This is my test string."
echo ${x##* }

string.

# Operator "%" means "delete from the right, to the first case of what follows."
x="This is my test string."
echo ${x% *}

This is my test

# Operator "%%" means "delete from the right, to the last case of what follows."
x="This is my test string."
$ echo ${x%% *}

This
```