bash find exec sh -c _ , inner working of symbols
up vote
2
down vote
favorite
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"' _ ;
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.
So the question is, how does _
assign variable to $1
? Is it possible to assign multiple variable to it with find/ or other commands?
bash shell find exec
add a comment |
up vote
2
down vote
favorite
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"' _ ;
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.
So the question is, how does _
assign variable to $1
? Is it possible to assign multiple variable to it with find/ or other commands?
bash shell find exec
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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"' _ ;
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.
So the question is, how does _
assign variable to $1
? Is it possible to assign multiple variable to it with find/ or other commands?
bash shell find exec
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"' _ ;
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.
So the question is, how does _
assign variable to $1
? Is it possible to assign multiple variable to it with find/ or other commands?
bash shell find exec
bash shell find exec
edited Nov 10 at 20:54
codeforester
17.1k83864
17.1k83864
asked Nov 10 at 4:33
Rocky Li
2,5521315
2,5521315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
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
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.
1
Thanks, this is very clear and helpful. Does this mean that _ is not necessary and I can just use $0 instead? Is there any benefit to use $1?
– Rocky Li
Nov 10 at 5:15
1
Also if I'm not wrong, the command starts one child/mini script for each of the file returned from find.
– Inian
Nov 10 at 5:30
2
@Inian Correct on both counts. You could use$0
to pass the filename, but$0
is normally the name of the program (script, executable, etc) being executed, and using it as the file being operating on can lead to some confusion. ("_" isn't really a very good program name either, but at least it's not actively misleading.) As for running the mini-script for each file, that's how-exec ... ;
works. If you use-exec ... +
, it'll run them in big batches, so the filenames will be$1
,$2
, ...$148
or whatever. You can do this, but then you need a processing loop in the mini-script.
– Gordon Davisson
Nov 10 at 6:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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
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.
1
Thanks, this is very clear and helpful. Does this mean that _ is not necessary and I can just use $0 instead? Is there any benefit to use $1?
– Rocky Li
Nov 10 at 5:15
1
Also if I'm not wrong, the command starts one child/mini script for each of the file returned from find.
– Inian
Nov 10 at 5:30
2
@Inian Correct on both counts. You could use$0
to pass the filename, but$0
is normally the name of the program (script, executable, etc) being executed, and using it as the file being operating on can lead to some confusion. ("_" isn't really a very good program name either, but at least it's not actively misleading.) As for running the mini-script for each file, that's how-exec ... ;
works. If you use-exec ... +
, it'll run them in big batches, so the filenames will be$1
,$2
, ...$148
or whatever. You can do this, but then you need a processing loop in the mini-script.
– Gordon Davisson
Nov 10 at 6:02
add a comment |
up vote
3
down vote
accepted
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
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.
1
Thanks, this is very clear and helpful. Does this mean that _ is not necessary and I can just use $0 instead? Is there any benefit to use $1?
– Rocky Li
Nov 10 at 5:15
1
Also if I'm not wrong, the command starts one child/mini script for each of the file returned from find.
– Inian
Nov 10 at 5:30
2
@Inian Correct on both counts. You could use$0
to pass the filename, but$0
is normally the name of the program (script, executable, etc) being executed, and using it as the file being operating on can lead to some confusion. ("_" isn't really a very good program name either, but at least it's not actively misleading.) As for running the mini-script for each file, that's how-exec ... ;
works. If you use-exec ... +
, it'll run them in big batches, so the filenames will be$1
,$2
, ...$148
or whatever. You can do this, but then you need a processing loop in the mini-script.
– Gordon Davisson
Nov 10 at 6:02
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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
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.
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
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.
answered Nov 10 at 5:08
Gordon Davisson
66.5k97792
66.5k97792
1
Thanks, this is very clear and helpful. Does this mean that _ is not necessary and I can just use $0 instead? Is there any benefit to use $1?
– Rocky Li
Nov 10 at 5:15
1
Also if I'm not wrong, the command starts one child/mini script for each of the file returned from find.
– Inian
Nov 10 at 5:30
2
@Inian Correct on both counts. You could use$0
to pass the filename, but$0
is normally the name of the program (script, executable, etc) being executed, and using it as the file being operating on can lead to some confusion. ("_" isn't really a very good program name either, but at least it's not actively misleading.) As for running the mini-script for each file, that's how-exec ... ;
works. If you use-exec ... +
, it'll run them in big batches, so the filenames will be$1
,$2
, ...$148
or whatever. You can do this, but then you need a processing loop in the mini-script.
– Gordon Davisson
Nov 10 at 6:02
add a comment |
1
Thanks, this is very clear and helpful. Does this mean that _ is not necessary and I can just use $0 instead? Is there any benefit to use $1?
– Rocky Li
Nov 10 at 5:15
1
Also if I'm not wrong, the command starts one child/mini script for each of the file returned from find.
– Inian
Nov 10 at 5:30
2
@Inian Correct on both counts. You could use$0
to pass the filename, but$0
is normally the name of the program (script, executable, etc) being executed, and using it as the file being operating on can lead to some confusion. ("_" isn't really a very good program name either, but at least it's not actively misleading.) As for running the mini-script for each file, that's how-exec ... ;
works. If you use-exec ... +
, it'll run them in big batches, so the filenames will be$1
,$2
, ...$148
or whatever. You can do this, but then you need a processing loop in the mini-script.
– Gordon Davisson
Nov 10 at 6:02
1
1
Thanks, this is very clear and helpful. Does this mean that _ is not necessary and I can just use $0 instead? Is there any benefit to use $1?
– Rocky Li
Nov 10 at 5:15
Thanks, this is very clear and helpful. Does this mean that _ is not necessary and I can just use $0 instead? Is there any benefit to use $1?
– Rocky Li
Nov 10 at 5:15
1
1
Also if I'm not wrong, the command starts one child/mini script for each of the file returned from find.
– Inian
Nov 10 at 5:30
Also if I'm not wrong, the command starts one child/mini script for each of the file returned from find.
– Inian
Nov 10 at 5:30
2
2
@Inian Correct on both counts. You could use
$0
to pass the filename, but $0
is normally the name of the program (script, executable, etc) being executed, and using it as the file being operating on can lead to some confusion. ("_" isn't really a very good program name either, but at least it's not actively misleading.) As for running the mini-script for each file, that's how -exec ... ;
works. If you use -exec ... +
, it'll run them in big batches, so the filenames will be $1
, $2
, ... $148
or whatever. You can do this, but then you need a processing loop in the mini-script.– Gordon Davisson
Nov 10 at 6:02
@Inian Correct on both counts. You could use
$0
to pass the filename, but $0
is normally the name of the program (script, executable, etc) being executed, and using it as the file being operating on can lead to some confusion. ("_" isn't really a very good program name either, but at least it's not actively misleading.) As for running the mini-script for each file, that's how -exec ... ;
works. If you use -exec ... +
, it'll run them in big batches, so the filenames will be $1
, $2
, ... $148
or whatever. You can do this, but then you need a processing loop in the mini-script.– Gordon Davisson
Nov 10 at 6:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236018%2fbash-find-exec-sh-c-inner-working-of-symbols%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown