sh 中的“${0%/*}”和“${0##*/}”
"${0%/*}" and "${0##*/}" in sh
这些是 brew 命令的摘录。
BREW_FILE_DIRECTORY=$(chdir "${0%/*}" && pwd -P)
export HOMEBREW_BREW_FILE="$BREW_FILE_DIRECTORY/${0##*/}"
${0%/*}
和 ${0##*/}
在 shell 中是什么意思?
这些是字符串操作。可以参考这个document
${string%substring} # Deletes shortest match of $substring from back of $string.
${string##substring} # Deletes longest match of $substring from front of $string.
有了这个,我们就可以解释运算符在您的代码中做了什么。
${0%/*}
假设 $0 是一个文件名,它会给你它所在的目录。它的工作方式与 dirname
命令相同。
${0##*/}
假设 $0 是一个文件名,它会给你没有前导路径的文件名。它的工作方式与 basename
命令相同。
这些是shell parameter expansions:
${var%/*}
- 删除最后一次出现的 /
. 之后的所有内容
${var##*/}
- 删除最后一次出现 /
之前的所有内容。
由于您在脚本中,[=16=]
指的是脚本本身的名称。
总而言之,这将返回您所在的脚本的路径或名称 运行。所以你在做:
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
来自上面提到的link(经过编辑的版本使它们更短):
${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.
关于 [=16=]
本身,参见 Bash reference manual -> 6.1 Invoking bash:
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, [=19=] 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.
这些是 brew 命令的摘录。
BREW_FILE_DIRECTORY=$(chdir "${0%/*}" && pwd -P)
export HOMEBREW_BREW_FILE="$BREW_FILE_DIRECTORY/${0##*/}"
${0%/*}
和 ${0##*/}
在 shell 中是什么意思?
这些是字符串操作。可以参考这个document
${string%substring} # Deletes shortest match of $substring from back of $string.
${string##substring} # Deletes longest match of $substring from front of $string.
有了这个,我们就可以解释运算符在您的代码中做了什么。
${0%/*}
假设 $0 是一个文件名,它会给你它所在的目录。它的工作方式与 dirname
命令相同。
${0##*/}
假设 $0 是一个文件名,它会给你没有前导路径的文件名。它的工作方式与 basename
命令相同。
这些是shell parameter expansions:
${var%/*}
- 删除最后一次出现的/
. 之后的所有内容
${var##*/}
- 删除最后一次出现/
之前的所有内容。
由于您在脚本中,[=16=]
指的是脚本本身的名称。
总而言之,这将返回您所在的脚本的路径或名称 运行。所以你在做:
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
来自上面提到的link(经过编辑的版本使它们更短):
${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.
关于 [=16=]
本身,参见 Bash reference manual -> 6.1 Invoking bash:
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, [=19=] 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.