bash bash中字符串的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17368067/
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
Length of string in bash
提问by AJP
How do you get the length of a string stored in a variable and assign that to another variable?
如何获取存储在变量中的字符串的长度并将其分配给另一个变量?
myvar="some string"
echo ${#myvar}
# 11
How do you set another variable to the output 11
?
你如何为输出设置另一个变量11
?
采纳答案by F. Hauri
UTF-8 string length
UTF-8 字符串长度
In addition to fedorqui's correct answer, I would like to show the difference between string length and byte length:
除了fedorqui的正确答案之外,我还想说明字符串长度和字节长度之间的区别:
myvar='Généralités'
chrlen=${#myvar}
oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#myvar}
LANG=$oLang LC_ALL=$oLcAll
printf "%s is %d char len, but %d bytes len.\n" "${myvar}" $chrlen $bytlen
will render:
将呈现:
Généralités is 11 char len, but 14 bytes len.
you could even have a look at stored chars:
你甚至可以看看存储的字符:
myvar='Généralités'
chrlen=${#myvar}
oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#myvar}
printf -v myreal "%q" "$myvar"
LANG=$oLang LC_ALL=$oLcAll
printf "%s has %d chars, %d bytes: (%s).\n" "${myvar}" $chrlen $bytlen "$myreal"
will answer:
会回答:
Généralités has 11 chars, 14 bytes: ($'G31n31ralit31s').
Nota:According to Isabell Cowan's comment, I've added setting to $LC_ALL
along with $LANG
.
注意:根据Isabell Cowan 的评论,我已经将设置$LC_ALL
与$LANG
.
Length of an argument
参数的长度
Argument work same as regular variables
参数工作与常规变量相同
strLen() {
local bytlen sreal oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#1}
printf -v sreal %q ""
LANG=$oLang LC_ALL=$oLcAll
printf "String '%s' is %d bytes, but %d chars len: %s.\n" "" $bytlen ${#1} "$sreal"
}
will work as
将作为
strLen théorème
String 'théorème' is 10 bytes, but 8 chars len: $'th31or30me'
Useful printf
correction tool:
有用的printf
校正工具:
If you:
如果你:
for string in Généralités Language Théorème Février "Left: ←" "Yin Yang ?";do
printf " - %-14s is %2d char length\n" "'$string'" ${#string}
done
- 'Généralités' is 11 char length
- 'Language' is 8 char length
- 'Théorème' is 8 char length
- 'Février' is 7 char length
- 'Left: ←' is 7 char length
- 'Yin Yang ?' is 10 char length
Not really pretty... For this, there is a little function:
不是很漂亮......为此,有一个小功能:
strU8DiffLen () {
local bytlen oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#1}
LANG=$oLang LC_ALL=$oLcAll
return $(( bytlen - ${#1} ))
}
Then now:
那么现在:
for string in Généralités Language Théorème Février "Left: ←" "Yin Yang ?";do
strU8DiffLen "$string"
printf " - %-$((14+$?))s is %2d chars length, but uses %2d bytes\n" \
"'$string'" ${#string} $((${#string}+$?))
done
- 'Généralités' is 11 chars length, but uses 14 bytes
- 'Language' is 8 chars length, but uses 8 bytes
- 'Théorème' is 8 chars length, but uses 10 bytes
- 'Février' is 7 chars length, but uses 8 bytes
- 'Left: ←' is 7 chars length, but uses 9 bytes
- 'Yin Yang ?' is 10 chars length, but uses 12 bytes
Unfortunely, this is not perfect!
不幸的是,这并不完美!
But there left some strange UTF-8 behaviour, like double-spaced chars, zero spaced chars, reverse deplacement and other that could not be as simple...
但是留下了一些奇怪的 UTF-8 行为,比如双倍行距字符、零行距字符、反向置换和其他不可能那么简单的行为......
Have a look at diffU8test.shor diffU8test.sh.txtfor more limitations.
查看diffU8test.sh或diffU8test.sh.txt了解更多限制。
回答by fedorqui 'SO stop harming'
To get the length of a string stored in a variable, say:
要获取存储在变量中的字符串的长度,请说:
myvar="some string"
size=${#myvar}
To confirm it was properly saved, echo
it:
为了确认它被正确保存,echo
它:
$ echo "$size"
11
回答by atesin
You can use:
您可以使用:
MYSTRING="abc123"
MYLENGTH=$(printf "%s" "$MYSTRING" | wc -c)
wc -c
orwc --bytes
for byte counts = Unicode characters are counted with 2, 3 or more bytes.wc -m
orwc --chars
for character counts = Unicode characters are counted single until they use more bytes.
wc -c
或wc --bytes
对于字节计数 = Unicode 字符按 2、3 或更多字节计数。wc -m
或者wc --chars
对于字符计数 = Unicode 字符被计算为单个,直到它们使用更多字节。
回答by dmatej
I wanted the simplest case, finally this is a result:
我想要最简单的情况,最后这是一个结果:
echo -n 'Tell me the length of this sentence.' | wc -m;
36
回答by Dick Guertin
If you want to use this with command line or function arguments, make sure you use size=${#1}
instead of size=${#$1}
. The second one may be more instinctual but is incorrect syntax.
如果您想将其与命令行或函数参数一起使用,请确保使用size=${#1}
代替size=${#$1}
。第二个可能更本能,但语法不正确。
回答by JGFMK
In response to the post starting:
回复帖子开始:
If you want to use this with command line or function arguments...
如果您想将其与命令行或函数参数一起使用...
with the code:
使用代码:
size=${#1}
There might be the case where you just want to check for a zero length argument and have no need to store a variable. I believe you can use this sort of syntax:
可能存在您只想检查零长度参数而无需存储变量的情况。我相信你可以使用这种语法:
if [ -z "" ]; then
#zero length argument
else
#non-zero length
fi
See GNUand wooledgefor a more complete list of Bash conditional expressions.
回答by thistleknot
Using your example provided
使用您提供的示例
#KISS (Keep it simple stupid)
size=${#myvar}
echo $size
回答by Mukesh Shakya
Here is couple of ways to calculate length of variable :
这里有几种计算变量长度的方法:
echo ${#VAR}
echo -n $VAR | wc -m
echo -n $VAR | wc -c
printf $VAR | wc -m
expr length $VAR
expr $VAR : '.*'
and to set the result in another variable just assign above command with back quote into another variable as following:
并将结果设置在另一个变量中,只需将带有反引号的上述命令分配给另一个变量,如下所示:
otherVar=`echo -n $VAR | wc -m`
echo $otherVar
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