Create an abbreviation for “2>/dev/null &”

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I'm trying to compose myself a bash script with the intent of it being used commonly in a terminal. It should start an application as a background process and discard it's stderr output. Here's what I got:



 for app in $@; do 
$app 2>/dev/null
done


It seemed to work just fine with bare applications started without parameters, like script.sh firefox gedit but failed to do the following:



script.sh "vlc video.mp4"


My question is: How can I enhance this basic script to handle applications which take parameters/files as their input? Maybe there already is a tool I can use?







share|improve this question























    up vote
    0
    down vote

    favorite












    I'm trying to compose myself a bash script with the intent of it being used commonly in a terminal. It should start an application as a background process and discard it's stderr output. Here's what I got:



     for app in $@; do 
    $app 2>/dev/null
    done


    It seemed to work just fine with bare applications started without parameters, like script.sh firefox gedit but failed to do the following:



    script.sh "vlc video.mp4"


    My question is: How can I enhance this basic script to handle applications which take parameters/files as their input? Maybe there already is a tool I can use?







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to compose myself a bash script with the intent of it being used commonly in a terminal. It should start an application as a background process and discard it's stderr output. Here's what I got:



       for app in $@; do 
      $app 2>/dev/null
      done


      It seemed to work just fine with bare applications started without parameters, like script.sh firefox gedit but failed to do the following:



      script.sh "vlc video.mp4"


      My question is: How can I enhance this basic script to handle applications which take parameters/files as their input? Maybe there already is a tool I can use?







      share|improve this question











      I'm trying to compose myself a bash script with the intent of it being used commonly in a terminal. It should start an application as a background process and discard it's stderr output. Here's what I got:



       for app in $@; do 
      $app 2>/dev/null
      done


      It seemed to work just fine with bare applications started without parameters, like script.sh firefox gedit but failed to do the following:



      script.sh "vlc video.mp4"


      My question is: How can I enhance this basic script to handle applications which take parameters/files as their input? Maybe there already is a tool I can use?









      share|improve this question










      share|improve this question




      share|improve this question









      asked May 3 at 13:02









      610

      32




      32




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          There are two issues:




          1. The use of $@ without quotes.



            This would make the loop iterate over vlc and video.mp4 as two separate items, even if these were within the same quoted string in the invocation of the script.




          2. Using a command in a variable.



            If the command is anything more complicated than a single simple command, then this won't work. You would have to eval the given string instead.



          Taking this into account, your script could look like



          #!/bin/sh

          for cmd do # or: for cmd in "$@"; do
          eval "$cmd" 2>/dev/null
          done


          Calling this as



          ./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'


          would first run echo "hello world", and when that finishes, it would open vim for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval (it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT occurs in a file). It is run as soon as the vim session exits.



          With this, you could even do



          ./script 'd=$HOME' 'ls "$d"'


          i.e., set variables that are used in later commands. This works because the commands invoked by eval are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.






          share|improve this answer























          • Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
            – 610
            May 4 at 11:18

















          up vote
          1
          down vote













          You're currently telling your script to handle each parameter as a separate application. So you are running:



          $ vlc


          Then:



          $ video.mp4



          You should make your script more like this:



          #!/bin/bash
          $@ 2>/dev/null


          Which, in my opinion, would be better suited as a function:



          func_name() 
          $@ 2>/dev/null




          Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.






          share|improve this answer























            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441543%2fcreate-an-abbreviation-for-2-dev-null%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            There are two issues:




            1. The use of $@ without quotes.



              This would make the loop iterate over vlc and video.mp4 as two separate items, even if these were within the same quoted string in the invocation of the script.




            2. Using a command in a variable.



              If the command is anything more complicated than a single simple command, then this won't work. You would have to eval the given string instead.



            Taking this into account, your script could look like



            #!/bin/sh

            for cmd do # or: for cmd in "$@"; do
            eval "$cmd" 2>/dev/null
            done


            Calling this as



            ./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'


            would first run echo "hello world", and when that finishes, it would open vim for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval (it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT occurs in a file). It is run as soon as the vim session exits.



            With this, you could even do



            ./script 'd=$HOME' 'ls "$d"'


            i.e., set variables that are used in later commands. This works because the commands invoked by eval are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.






            share|improve this answer























            • Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
              – 610
              May 4 at 11:18














            up vote
            5
            down vote



            accepted










            There are two issues:




            1. The use of $@ without quotes.



              This would make the loop iterate over vlc and video.mp4 as two separate items, even if these were within the same quoted string in the invocation of the script.




            2. Using a command in a variable.



              If the command is anything more complicated than a single simple command, then this won't work. You would have to eval the given string instead.



            Taking this into account, your script could look like



            #!/bin/sh

            for cmd do # or: for cmd in "$@"; do
            eval "$cmd" 2>/dev/null
            done


            Calling this as



            ./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'


            would first run echo "hello world", and when that finishes, it would open vim for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval (it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT occurs in a file). It is run as soon as the vim session exits.



            With this, you could even do



            ./script 'd=$HOME' 'ls "$d"'


            i.e., set variables that are used in later commands. This works because the commands invoked by eval are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.






            share|improve this answer























            • Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
              – 610
              May 4 at 11:18












            up vote
            5
            down vote



            accepted







            up vote
            5
            down vote



            accepted






            There are two issues:




            1. The use of $@ without quotes.



              This would make the loop iterate over vlc and video.mp4 as two separate items, even if these were within the same quoted string in the invocation of the script.




            2. Using a command in a variable.



              If the command is anything more complicated than a single simple command, then this won't work. You would have to eval the given string instead.



            Taking this into account, your script could look like



            #!/bin/sh

            for cmd do # or: for cmd in "$@"; do
            eval "$cmd" 2>/dev/null
            done


            Calling this as



            ./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'


            would first run echo "hello world", and when that finishes, it would open vim for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval (it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT occurs in a file). It is run as soon as the vim session exits.



            With this, you could even do



            ./script 'd=$HOME' 'ls "$d"'


            i.e., set variables that are used in later commands. This works because the commands invoked by eval are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.






            share|improve this answer















            There are two issues:




            1. The use of $@ without quotes.



              This would make the loop iterate over vlc and video.mp4 as two separate items, even if these were within the same quoted string in the invocation of the script.




            2. Using a command in a variable.



              If the command is anything more complicated than a single simple command, then this won't work. You would have to eval the given string instead.



            Taking this into account, your script could look like



            #!/bin/sh

            for cmd do # or: for cmd in "$@"; do
            eval "$cmd" 2>/dev/null
            done


            Calling this as



            ./script 'echo "hello world"' 'vim "$HOME/.profile"' 'tr a-z A-Z <"$HOME/.profile" | grep -c EXPORT'


            would first run echo "hello world", and when that finishes, it would open vim for editing the named file in that second command. The last command is more complex but handled by the fact that we use eval (it just changes all alphabetic characters to uppercase and counts the number of times the string EXPORT occurs in a file). It is run as soon as the vim session exits.



            With this, you could even do



            ./script 'd=$HOME' 'ls "$d"'


            i.e., set variables that are used in later commands. This works because the commands invoked by eval are run in the same environment as the script. This would not work if you start the commands as background tasks though, as the title of your question suggests.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited May 3 at 13:52


























            answered May 3 at 13:08









            Kusalananda

            102k13199316




            102k13199316











            • Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
              – 610
              May 4 at 11:18
















            • Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
              – 610
              May 4 at 11:18















            Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
            – 610
            May 4 at 11:18




            Thank you for the clarifying answer, I'll definitely make use of 'eval' now that I know about it.
            – 610
            May 4 at 11:18












            up vote
            1
            down vote













            You're currently telling your script to handle each parameter as a separate application. So you are running:



            $ vlc


            Then:



            $ video.mp4



            You should make your script more like this:



            #!/bin/bash
            $@ 2>/dev/null


            Which, in my opinion, would be better suited as a function:



            func_name() 
            $@ 2>/dev/null




            Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.






            share|improve this answer



























              up vote
              1
              down vote













              You're currently telling your script to handle each parameter as a separate application. So you are running:



              $ vlc


              Then:



              $ video.mp4



              You should make your script more like this:



              #!/bin/bash
              $@ 2>/dev/null


              Which, in my opinion, would be better suited as a function:



              func_name() 
              $@ 2>/dev/null




              Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                You're currently telling your script to handle each parameter as a separate application. So you are running:



                $ vlc


                Then:



                $ video.mp4



                You should make your script more like this:



                #!/bin/bash
                $@ 2>/dev/null


                Which, in my opinion, would be better suited as a function:



                func_name() 
                $@ 2>/dev/null




                Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.






                share|improve this answer















                You're currently telling your script to handle each parameter as a separate application. So you are running:



                $ vlc


                Then:



                $ video.mp4



                You should make your script more like this:



                #!/bin/bash
                $@ 2>/dev/null


                Which, in my opinion, would be better suited as a function:



                func_name() 
                $@ 2>/dev/null




                Note: this will only allow you to run one command at a time. I'm not sure if your original intent was to pass multiple commands at once to your script, if so I will delete.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited May 3 at 13:19


























                answered May 3 at 13:08









                Jesse_b

                10.3k22658




                10.3k22658






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441543%2fcreate-an-abbreviation-for-2-dev-null%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?