bash bash的字符串长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17750940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:57:52  来源:igfitidea点击:

String length of bash

bashshell

提问by user2443218

I have written this to find the length of the string, but it doesn't show the length of the string.

我写这个是为了找到字符串的长度,但它没有显示字符串的长度。

Is there something wrong with what I have written? I'm just a beginner in bash.

我写的有什么问题吗?我只是 bash 的初学者。

Str=abcdefghj

echo "Str is:" `expr length $Str` "characters long"

回答by iruvar

This can be done natively in bash, no need to resort to expr

这可以在 bash 中本地完成,无需求助于 expr

echo ${#Str}

回答by Mukesh Shakya

Here is couple of ways to calculate length of variable :

这里有几种计算变量长度的方法:

echo ${#Str}
echo -n $Str | wc -m
echo -n $Str | wc -c
printf $Str | wc -m
expr length $Str
expr $Str : '.*'

Reference: http://techopsbook.blogspot.in/2017/09/how-to-find-length-of-string-variable.html

参考:http: //techopsbook.blogspot.in/2017/09/how-to-find-length-of-string-variable.html

https://youtu.be/J6-Fkkj7f_8

https://youtu.be/J6-Fkkj7f_8

回答by Angel García

: Try something like this,

: 试试这样的,

echo "Str is: expr length $Strcharacters long"

echo "Str 是:expr length $Str字符长"