How can we get the length of a string in shell scripting?

In the shell, you can retrieve the length of a string using the `expr length` command or by using the `${#string}` syntax. Below are examples of both methods: 1. Using the `expr length` command:

str="Hello, World!"
length=$(expr length "$str")
echo "字符串长度为: $length"

Output:

字符串长度为: 13

2. Use the `${#string}` syntax:

str="Hello, World!"
length=${#str}
echo "字符串长度为: $length"

Output:

字符串长度为: 13
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds