bash find exec sh -c _ , inner working of symbols









up vote
2
down vote

favorite
1












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?










share|improve this question



























    up vote
    2
    down vote

    favorite
    1












    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?










    share|improve this question

























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 20:54









      codeforester

      17.1k83864




      17.1k83864










      asked Nov 10 at 4:33









      Rocky Li

      2,5521315




      2,5521315






















          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.






          share|improve this answer
















          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer
















          • 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














          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.






          share|improve this answer
















          • 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












          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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












          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20