stderr redirection by reading a file [duplicate]

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











up vote
2
down vote

favorite













This question already has an answer here:



  • How to make bash substitution $(<“filename”) silent

    4 answers



I read content of the file into variable like this



var=$(<somefile)


But if the file doesn't exist I get accordingly an error message



bash: somefile: No such file or directory


Is it possible to redirect stderr to >/dev/null without using cat command?







share|improve this question












marked as duplicate by Jeff Schaller, Community♦ Mar 14 at 10:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    2
    down vote

    favorite













    This question already has an answer here:



    • How to make bash substitution $(<“filename”) silent

      4 answers



    I read content of the file into variable like this



    var=$(<somefile)


    But if the file doesn't exist I get accordingly an error message



    bash: somefile: No such file or directory


    Is it possible to redirect stderr to >/dev/null without using cat command?







    share|improve this question












    marked as duplicate by Jeff Schaller, Community♦ Mar 14 at 10:51


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite












      This question already has an answer here:



      • How to make bash substitution $(<“filename”) silent

        4 answers



      I read content of the file into variable like this



      var=$(<somefile)


      But if the file doesn't exist I get accordingly an error message



      bash: somefile: No such file or directory


      Is it possible to redirect stderr to >/dev/null without using cat command?







      share|improve this question













      This question already has an answer here:



      • How to make bash substitution $(<“filename”) silent

        4 answers



      I read content of the file into variable like this



      var=$(<somefile)


      But if the file doesn't exist I get accordingly an error message



      bash: somefile: No such file or directory


      Is it possible to redirect stderr to >/dev/null without using cat command?





      This question already has an answer here:



      • How to make bash substitution $(<“filename”) silent

        4 answers









      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 14 at 8:37









      renton

      111




      111




      marked as duplicate by Jeff Schaller, Community♦ Mar 14 at 10:51


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Jeff Schaller, Community♦ Mar 14 at 10:51


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          In this case, either use cat:



          var=$( cat somefile 2>/dev/null )


          or test for file existence first:



          if [ -f somefile ]; then
          var=$(<somefile)
          fi


          To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):



          if cp somefile myname 2>/dev/null; then
          var=$(<myname)
          rm -f myname
          fi



          The following will not work:



          var=$(<somefile) 2>/dev/null
          var=$(<somefile 2>/dev/null)





          share|improve this answer






















          • This is the pettiest gripe, but the second fix has a race condition...
            – Michael Homer
            Mar 14 at 9:04










          • @MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the cat is probably best (and also portable).
            – Kusalananda
            Mar 14 at 9:25

















          up vote
          2
          down vote













          You can use:



           var=$(<somefile); 2> /dev/null





          share|improve this answer




















          • Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
            – Kusalananda
            Mar 14 at 10:28










          • @Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
            – Stéphane Chazelas
            Mar 14 at 10:35

















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote













          In this case, either use cat:



          var=$( cat somefile 2>/dev/null )


          or test for file existence first:



          if [ -f somefile ]; then
          var=$(<somefile)
          fi


          To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):



          if cp somefile myname 2>/dev/null; then
          var=$(<myname)
          rm -f myname
          fi



          The following will not work:



          var=$(<somefile) 2>/dev/null
          var=$(<somefile 2>/dev/null)





          share|improve this answer






















          • This is the pettiest gripe, but the second fix has a race condition...
            – Michael Homer
            Mar 14 at 9:04










          • @MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the cat is probably best (and also portable).
            – Kusalananda
            Mar 14 at 9:25














          up vote
          4
          down vote













          In this case, either use cat:



          var=$( cat somefile 2>/dev/null )


          or test for file existence first:



          if [ -f somefile ]; then
          var=$(<somefile)
          fi


          To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):



          if cp somefile myname 2>/dev/null; then
          var=$(<myname)
          rm -f myname
          fi



          The following will not work:



          var=$(<somefile) 2>/dev/null
          var=$(<somefile 2>/dev/null)





          share|improve this answer






















          • This is the pettiest gripe, but the second fix has a race condition...
            – Michael Homer
            Mar 14 at 9:04










          • @MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the cat is probably best (and also portable).
            – Kusalananda
            Mar 14 at 9:25












          up vote
          4
          down vote










          up vote
          4
          down vote









          In this case, either use cat:



          var=$( cat somefile 2>/dev/null )


          or test for file existence first:



          if [ -f somefile ]; then
          var=$(<somefile)
          fi


          To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):



          if cp somefile myname 2>/dev/null; then
          var=$(<myname)
          rm -f myname
          fi



          The following will not work:



          var=$(<somefile) 2>/dev/null
          var=$(<somefile 2>/dev/null)





          share|improve this answer














          In this case, either use cat:



          var=$( cat somefile 2>/dev/null )


          or test for file existence first:



          if [ -f somefile ]; then
          var=$(<somefile)
          fi


          To avoid the race condition in the last example (the file might theoretically disappear between the test and reading it):



          if cp somefile myname 2>/dev/null; then
          var=$(<myname)
          rm -f myname
          fi



          The following will not work:



          var=$(<somefile) 2>/dev/null
          var=$(<somefile 2>/dev/null)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 14 at 10:05

























          answered Mar 14 at 8:48









          Kusalananda

          103k13201318




          103k13201318











          • This is the pettiest gripe, but the second fix has a race condition...
            – Michael Homer
            Mar 14 at 9:04










          • @MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the cat is probably best (and also portable).
            – Kusalananda
            Mar 14 at 9:25
















          • This is the pettiest gripe, but the second fix has a race condition...
            – Michael Homer
            Mar 14 at 9:04










          • @MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the cat is probably best (and also portable).
            – Kusalananda
            Mar 14 at 9:25















          This is the pettiest gripe, but the second fix has a race condition...
          – Michael Homer
          Mar 14 at 9:04




          This is the pettiest gripe, but the second fix has a race condition...
          – Michael Homer
          Mar 14 at 9:04












          @MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the cat is probably best (and also portable).
          – Kusalananda
          Mar 14 at 9:25




          @MichaelHomer It has. The file might theoretically disappear between the test and reading it. Another way would be to copy the file and read it if that succeeded, but the cat is probably best (and also portable).
          – Kusalananda
          Mar 14 at 9:25












          up vote
          2
          down vote













          You can use:



           var=$(<somefile); 2> /dev/null





          share|improve this answer




















          • Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
            – Kusalananda
            Mar 14 at 10:28










          • @Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
            – Stéphane Chazelas
            Mar 14 at 10:35














          up vote
          2
          down vote













          You can use:



           var=$(<somefile); 2> /dev/null





          share|improve this answer




















          • Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
            – Kusalananda
            Mar 14 at 10:28










          • @Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
            – Stéphane Chazelas
            Mar 14 at 10:35












          up vote
          2
          down vote










          up vote
          2
          down vote









          You can use:



           var=$(<somefile); 2> /dev/null





          share|improve this answer












          You can use:



           var=$(<somefile); 2> /dev/null






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 14 at 10:22









          Stéphane Chazelas

          280k53515847




          280k53515847











          • Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
            – Kusalananda
            Mar 14 at 10:28










          • @Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
            – Stéphane Chazelas
            Mar 14 at 10:35
















          • Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
            – Kusalananda
            Mar 14 at 10:28










          • @Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
            – Stéphane Chazelas
            Mar 14 at 10:35















          Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
          – Kusalananda
          Mar 14 at 10:28




          Hmm... yes, that would be best. Didn't we have this in an answer the other day (I can't find it)?
          – Kusalananda
          Mar 14 at 10:28












          @Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
          – Stéphane Chazelas
          Mar 14 at 10:35




          @Kusalananda, yes, I couldn't find it either, but I'm pretty sure there's a recent dup too.
          – Stéphane Chazelas
          Mar 14 at 10:35


          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?