% 在 Linux shell 字符串中做什么?

Nis*_*ach 30 linux shell sh

在 Linux shell 中, % 做什么,例如:

for file in *.png.jpg; do
  mv "$file" "${file%.png.jpg}.jpg"
done
Run Code Online (Sandbox Code Playgroud)

Mar*_*ost 31

%在模式被用来${variable%substring}将返回的内容variable与最短次数substring从后面删除variable

此函数支持通配符模式 - 这就是它接受星号(星号)作为零个或多个字符的替代的原因。

应该提到的是,这是 Bash 特定的 - 其他 linux shell 不一定包含此功能。

如果您想了解有关 Bash 中字符串操作的更多信息,我强烈建议您阅读页面。在其他方便的功能中,它 - 例如 - 解释了它的%%作用:)

编辑:我忘了提,当它在模式的二手$((variable%number))$((variable1%$variable2))%字符会模运算符的功能。DavidPostill 在他的回答中有更具体的文档链接。

%在不同的上下文中使用,它应该被视为唯一的规则字符。

  • 您链接到的指南非常*不*推荐[大多数 Unix 和 Linux 堆栈交换用户](http://unix.stackexchange.com/q/305358/135943)。我推荐 [Wooledge Bash 指南](http://mywiki.wooledge.org/BashGuide)。 (5认同)
  • 删除前缀和后缀的操作符是 [standard](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02),所以它们应该可以在任何 `sh` 兼容的 shell 中使用,而不仅仅是猛击。(虽然可能不在 `csh` 等中)。 (3认同)
  • 运算符支持*通配符模式*,而不是正则表达式。 (2认同)

Ste*_*ven 9

Bash 参考手册:Shell 参数扩展

${parameter%word}
${parameter%%word}

这个被扩展以产生一个模式,就像在文件名扩展中一样。如果模式匹配parameter扩展值的尾随部分,则扩展的结果是删除了最短匹配模式(案例)或最长匹配模式(案例)的参数值。如果参数是或模式移除操作依次应用于每个位置参数,扩展就是结果列表。如果参数是一个带有 或下标的数组变量‘%’‘%%’‘@’‘*’,‘@’‘*’, 模式移除操作依次应用于数组的每个成员,扩展就是结果列表。

  • 注意包括 % 在内的一些参数扩展是许多 shell 支持的 posix 功能。 (2认同)

Nis*_*ach 7

通过试验,我发现当字符串用大括号(大括号)括起来时,会丢弃 % 之后的匹配项。

为了显示:

touch abcd         # Create file abcd

for file in ab*; do
 echo $file        # echoes the filename
 echo $file%       # echoes the filename plus "%"
 echo ${file%}     # echoes the filename
 echo "${file%}"   # echoes the filename
 echo
 echo "${file%c*}" # Discard anything after % matching c*
 echo "${file%*}"  # * is not greedy
 echo ${file%c*}   # Without quotes works too
 echo "${file%c}"  # No match after %, no effect
 echo $file%c*     # Without {} fails
done
Run Code Online (Sandbox Code Playgroud)

这是输出:

abcd
abcd%
abcd
abcd

ab
abcd
ab
abcd
abcd%c*
Run Code Online (Sandbox Code Playgroud)


Dav*_*ill 7

在 Linux shell ( bash) 中,有什么作用%

for file in *.png.jpg; do
  mv "$file" "${file%.png.jpg}.jpg"
done
Run Code Online (Sandbox Code Playgroud)

在这种特殊情况下,%模式匹配运算符(注意它也可以是运算符)。


模式匹配运算符

${var%$Pattern}, ${var%%$Pattern}

${var%$Pattern}从匹配的后端$var的最短部分中删除。$Pattern$var

${var%%$Pattern}从匹配的后端$var的最长部分中删除。$Pattern$var

示例:参数替换中的模式匹配

#!/bin/bash
# patt-matching.sh

# Pattern matching  using the # ## % %% parameter substitution operators.

var1=abcd12345abc6789
pattern1=a*c  # * (wild card) matches everything between a - c.

echo
echo "var1 = $var1"           # abcd12345abc6789
echo "var1 = ${var1}"         # abcd12345abc6789
                              # (alternate form)
echo "Number of characters in ${var1} = ${#var1}"
echo

echo "pattern1 = $pattern1"   # a*c  (everything between 'a' and 'c')
echo "--------------"
echo '${var1#$pattern1}  =' "${var1#$pattern1}"    #         d12345abc6789
# Shortest possible match, strips out first 3 characters  abcd12345abc6789
#                                     ^^^^^               |-|
echo '${var1##$pattern1} =' "${var1##$pattern1}"   #                  6789      
# Longest possible match, strips out first 12 characters  abcd12345abc6789
#                                    ^^^^^                |----------|

echo; echo; echo

pattern2=b*9            # everything between 'b' and '9'
echo "var1 = $var1"     # Still  abcd12345abc6789
echo
echo "pattern2 = $pattern2"
echo "--------------"
echo '${var1%pattern2}  =' "${var1%$pattern2}"     #     abcd12345a
# Shortest possible match, strips out last 6 characters  abcd12345abc6789
#                                     ^^^^                         |----|
echo '${var1%%pattern2} =' "${var1%%$pattern2}"    #     a
# Longest possible match, strips out last 12 characters  abcd12345abc6789
#                                    ^^^^                 |-------------|

# Remember, # and ## work from the left end (beginning) of string,
#           % and %% work from the right end.

echo

exit 0
Run Code Online (Sandbox Code Playgroud)

参数替换


模运算符

%

modulo 或 mod(返回整数除法运算的余数)

bash$ expr 5 % 3
2
Run Code Online (Sandbox Code Playgroud)

5/3 = 1,余数为 2

来源运营商