有人可以在本bash末尾解释符号的内部工作原理:"_ {} \;" [英] Can someone explain the inner working of the symbols at the end of this bash: “_ {} \;”
问题描述
我在shell中运行了以下命令,将.HEIC
文件批量转换为.JPG
文件,该命令成功,但是有一部分我不理解:
I ran the following command in shell to batch convert .HEIC
files to .JPG
files, the command is successful, however there's a part of it I don't understand:
find . -name '*.HEIC' -exec sh -c 'magick convert $1 "${1%.HEIC}.JPG"' _ {} \;
显然,_ {}
用于将find
结果分配给$1
,但是如何?我在Google或这里都找不到解释,并且man find
没有运气.答案很可能在这里,但是这些符号并不是很好地搜索.
Apparently _ {}
acts to assign find
result to $1
, but how? I can't find an explanation on google nor here, and didn't have any luck with man find
. It's entirely possible that answers were here but these symbols are not very nice to search for.
所以问题是,_ {}
如何将变量分配给$1
?是否可以使用find/或其他命令为其分配多个变量?
So the question is, how does _ {}
assign variable to $1
? Is it possible to assign multiple variable to it with find/ or other commands?
推荐答案
_ {}
如何将文件名分配给$1
涉及两件事.首先,是find
的-exec
的工作方式:它作为命令运行以下参数(直至转义的;
),但将{}
替换为找到的文件的路径.因此,如果找到./somefile.HEIC
,它将运行与命令等效的命令:
There are two things involved in how _ {}
assigns the filename to $1
. First, is how the find
's -exec
works: it runs the following arguments (up to the escaped ;
) as a command, but with {}
replaced with the path to the file it found. Thus, if it finds ./somefile.HEIC
, it'll run the equivalent of the command:
sh -c 'magick convert $1 "${1%.HEIC}.JPG"' _ ./somefile.HEIC
第二部分是sh
命令. sh
可以做很多事情,但是如果给它一个-c
选项,它将紧随其后的参数(magick convert $1 "${1%.HEIC}.JPG"
)作为要解析和运行的命令字符串,有点像一个迷你脚本.之后的参数将作为该小型脚本的参数,以$0
开头.在这种情况下,这意味着它将在$0
设置为_
且$1
设置为"./somefile.HEIC"的情况下运行小型脚本.如果提供了更多参数,则为$2
,$3
等.
The second part is the sh
command. sh
can do a number of things, but if it's given a -c
option, it takes the immediately following argument (magick convert $1 "${1%.HEIC}.JPG"
) as a command string to parse and run, sort of like a little mini-script. The arguments after that are taken as arguments to that mini-script, starting with $0
. In this case, that means it runs the mini-script with $0
set to _
, and $1
set to "./somefile.HEIC". If more arguments were supplied, they'd be $2
, $3
, etc.
这篇关于有人可以在本bash末尾解释符号的内部工作原理:"_ {} \;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!