" $ {0%/*}"和"$ {0 ## */}";在sh [英] "${0%/*}" and "${0##*/}" in sh

查看:73
本文介绍了" $ {0%/*}"和"$ {0 ## */}";在sh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是Brew命令的摘录.

These are excerpts from a brew command.

BREW_FILE_DIRECTORY=$(chdir "${0%/*}" && pwd -P)
export HOMEBREW_BREW_FILE="$BREW_FILE_DIRECTORY/${0##*/}"

$ {0%/*} $ {0 ## */} 在shell中是什么意思?

What do ${0%/*} and ${0##*/} mean in shell?

推荐答案

这些是shell

  • $ {var%/*} -删除最后一次出现的/之后的所有内容.
  • $ {var ## */} -删除所有直到最后一次出现的/.
    • ${var%/*} - remove everything after the last occurrence of /.
    • ${var##*/} - remove everything up to the last occurrence of /.

    由于您处于脚本中,因此 $ 0 是指脚本本身的名称.

    Since you are in a script, $0 refers to the name of the script itself.

    一起,这将返回您正在运行的脚本的路径或名称.因此,您正在做某事:

    All together, this is returning either the path or the name of the script that you are running. So your are kind of doing:

    BREW_FILE_DIRECTORY=$(chdir <path_of_script> && pwd -P)
    export HOMEBREW_BREW_FILE="$BREW_FILE_DIRECTORY/<script_name>"
    

    测试

    $ r="hello/how/are/you"
    $ echo ${r%/*}
    hello/how/are
    $ echo ${r##*/}
    you
    

    从上述链接(经过编辑的版本可以使它们更短):

    From the above-mentioned link (edited versions to make them shorter):

    $ {parameter ## word}

    单词被扩展以产生与文件名相同的模式扩张.如果模式与扩展值的开头匹配的参数,则展开的结果就是展开的值具有最长匹配模式的参数("##"大小写)已删除.如果参数为"@"或"*",则模式删除操作为依次应用于每个位置参数,展开为结果列表.

    The word is expanded to produce a pattern just as in filename expansion. If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list.

    $ {parameter%word}

    单词被扩展以产生一个与文件名相同的模式扩张.如果该模式与扩展后的尾部匹配参数的值,则扩展的结果为匹配模式最短(%"大小写)的参数已删除.如果参数为"@"或"*",则将应用模式删除操作依次转到每个位置参数,展开为结果列表.

    The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list.

    关于 $ 0 本身,请参见 Bash参考手册-> 6.1调用Bash :

    如果在选项处理后仍保留参数,则-c或-c都不提供了-s选项,假定第一个参数为包含Shell命令的文件的名称(请参阅Shell脚本).什么时候以这种方式调用Bash,将$ 0设置为文件名,并将位置参数设置为其余参数.重击从该文件读取并执行命令,然后退出.

    If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands (see Shell Scripts). When Bash is invoked in this fashion, $0 is set to the name of the file, and the positional parameters are set to the remaining arguments. Bash reads and executes commands from this file, then exits.

    这篇关于&quot; $ {0%/*}&quot;和"$ {0 ## */}";在sh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆