string 如何检查字符串在 Bash shell 中是否有空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1473981/
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-09 00:32:36  来源:igfitidea点击:

How to check if a string has spaces in Bash shell

bashshellstring

提问by derrdji

Say a string might be like "a b '' c '' d". How can I check that there is single/double quote and space contained in the string?

说一个字符串可能像"a b '' c '' d". 如何检查字符串中是否包含单/双引号和空格?

采纳答案by Steve B.

case "$var" in  
     *\ * )
           echo "match"
          ;;
       *)
           echo "no match"
           ;;
esac

回答by Paused until further notice.

You can use regular expressions in bash:

您可以在 bash 中使用正则表达式:

string="a b '' c '' d"
if [[ "$string" =~ \ |\' ]]    #  slightly more readable: if [[ "$string" =~ ( |\') ]]
then
   echo "Matches"
else
   echo "No matches"
fi

Edit:

编辑:

For reasons obvious above, it's better to put the regex in a variable:

由于上面显而易见的原因,最好将正则表达式放在变量中:

pattern=" |'"
if [[ $string =~ $pattern ]]

And quotes aren't necessary inside double square brackets. They can't be used on the right or the regex is changed to a literal string.

并且双方括号内不需要引号。它们不能在右侧使用,或者正则表达式更改为文字字符串。

回答by codeforester

You could do this, without the need for any backslashes or external commands:

您可以这样做,而无需任何反斜杠或外部命令:

# string matching

if [[ $string = *" "* ]]; then
  echo "string contains one more spaces"
else
  echo "string doesn't contain spaces"
fi

# regex matching

re="[[:space:]]+"
if [[ $string =~ $re ]]; then
  echo "string contains one or more spaces"
else
  echo "string doesn't contain spaces"
fi


Related:

有关的:

回答by glenn Hymanman

[[ "$str" = "${str%[[:space:]]*}" ]] && echo "no spaces" || echo "has spaces"

回答by Paul

string="a b '' c '' d"
if [ "$string" == "${string//[\' ]/}" ]
then 
   echo did not contain space or single quote
else
   echo did contain space or single quote
fi

回答by alex tingle

The portable way to do this is with grep:

这样做的便携式方法是grep

S="a b '' c '' d"
if echo $S | grep -E '[ "]' >/dev/null
then
  echo "It's a match"
fi

...a bit ugly, but guaranteed to work everywhere.

...有点难看,但保证可以在任何地方工作。

回答by Grzegorz Oledzki

How about an approach similar to:

类似的方法如何:

$ A="some string"; echo $A | grep \  | wc -l
1
$ A="somestring"; echo $A | grep \  | wc -l
0

?

?

回答by ezpz

function foo() {
    echo "String: $*"
    SPACES=$(($#-1))
    echo "Spaces: $SPACES"
    QUOTES=0
    for i in $*; do
        if [ "$i" == "'" ]; then
            QUOTES=$((QUOTES+1))
        fi
    done
    echo "Quotes: $QUOTES"
    echo
}

S="string with spaces"
foo $S
S="single' 'quotes"
foo $S
S="single '' quotes"
foo $S
S="single ' ' quotes"
foo $S

yields:

产量:

String: string with spaces
Spaces: 2
Quotes: 0

String: single' 'quotes
Spaces: 1
Quotes: 0

String: single '' quotes
Spaces: 2
Quotes: 0

String: single ' ' quotes
Spaces: 3
Quotes: 2

回答by rgiannico

What about this:

那这个呢:

[[ $var == ${var//[ \"]/_} ]] && echo "quotes or spaces not found"

or if you like this:

或者如果你喜欢这个:

if [[ $var == ${var//[ \"]/_} ]] ; then  
   echo "quotes or spaces not found" 
else
   echo "found quotes or spaces"
fi

Explanation:I'm evaluating a comparison between the variable ${var} and the variable ${var} itself after a on-the-fly non-destructive string substitution of all the quotes and spaces with an underscore.

说明:我正在评估变量 ${var} 和变量 ${var} 本身之间的比较,在对所有引号和空格进行动态非破坏性字符串替换后,带有下划线。

Examples:

例子:

${var// /_}  # Substitute all spaces with underscores

The following code substitute all characters between the squared brackets (space and quotes) with an underscore. Note that quotes has to be protected with backslash:

以下代码用下划线替换方括号(空格和引号)之间的所有字符。请注意,引号必须用反斜杠保护:

${var//[ \"]/_}  

回答by dz.

I do wonder why nobody mentioned the [:space:] set. Usually your not only interested in detecting the space character. I often need to detect any white space, e.g. TAB. The "grep" example would look like this:

我确实想知道为什么没有人提到 [:space:] 集。通常您不仅对检测空格字符感兴趣。我经常需要检测任何空白,例如 TAB。“grep”示例如下所示:

$ echo " " | egrep -q "[:space:]" && echo "Has no Whitespace" || echo "Has Whitespace"
Has Whitespace
$ echo "a" | egrep -q "[:space:]" && echo "Has no Whitespace" || echo "Has Whitespace"
Has no Whitespace