如何在xargs中返回字符串长度(特殊字符)

时间:2022-02-27 22:16:52
#!/bin/bash

a=ača

echo ${#a}                                 #returns 3
echo $a | xargs -rL1 sh -c 'echo ${#0}';   #returns 4

In first echo it gives me right length of string. What should I do, to return right length of string in second echo? (I want length 3 in both cases)

在第一回波中,它给出了正确的弦长度。我该怎么做,在第二次回波中返回正确的字符串长度?(两种情况的长度都是3)

In my program I have a lot of lines, which I have to edit, that's why I use xargs.

在我的程序中有很多行,我必须编辑它们,这就是为什么我使用xargs。

1 个解决方案

#1


3  

The problem is not xargs. The problem is sh. If you want the correct (unicode) length, you need to use a shell that supports unicode. Observe:

问题不在于xargs。问题是sh.如果想要正确的(unicode)长度,需要使用支持unicode的shell。观察:

$ echo $a | xargs -rL1 sh -c 'echo ${#0}'
4
$ echo $a | xargs -rL1 bash -c 'echo ${#0}'
3

On debian-like systems, the default shell, /bin/sh, is actually dash which does not understand unicode. As shown above, simply replacing sh with bash solves the problem.

在类似debian的系统中,默认的shell /bin/sh实际上是不理解unicode的dash。如上所示,简单地用bash替换sh解决了这个问题。

#1


3  

The problem is not xargs. The problem is sh. If you want the correct (unicode) length, you need to use a shell that supports unicode. Observe:

问题不在于xargs。问题是sh.如果想要正确的(unicode)长度,需要使用支持unicode的shell。观察:

$ echo $a | xargs -rL1 sh -c 'echo ${#0}'
4
$ echo $a | xargs -rL1 bash -c 'echo ${#0}'
3

On debian-like systems, the default shell, /bin/sh, is actually dash which does not understand unicode. As shown above, simply replacing sh with bash solves the problem.

在类似debian的系统中,默认的shell /bin/sh实际上是不理解unicode的dash。如上所示,简单地用bash替换sh解决了这个问题。