【问题标题】:macOS Mojave version 10.14.1 bash-3.2 expr: syntax errormacOS Mojave 版本 10.14.1 bash-3.2 expr:语法错误
【发布时间】:2023-03-24 16:34:01
【问题描述】:

我遇到这样的错误:

expr: syntax error

我的脚本是:

#!/bin/bash
echo `expr index "sarasara"  a`

如何解决它。

【问题讨论】:

    标签: bash macos shell expr


    【解决方案1】:

    $ man expr 最后给出了这个:

    根据 POSIX 标准,使用字符串参数lengthsubstrindexmatch 会产生未定义的结果。在这个版本的 expr,这些参数被视为它们各自的字符串值。

    也就是说,macOS 的expr 不支持index

    【讨论】:

    • 在 Bash 中使用 expr 无论如何都是疯狂的。你实际上想要完成什么? var=${other%%a*} 将产生一个other 的子字符串,其长度表示找到第一个a$other 的距离。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2011-06-30
    • 1970-01-01
    • 2019-04-20
    相关资源
    最近更新 更多