What does -f mean in an if statement in a bash script?

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











up vote
2
down vote

favorite












Trying to understand this piece of code:



if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi


I'm not sure what the -f means exactly.







share|improve this question























    up vote
    2
    down vote

    favorite












    Trying to understand this piece of code:



    if [ -f /etc/bashrc ]; then
    . /etc/bashrc
    fi


    I'm not sure what the -f means exactly.







    share|improve this question





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Trying to understand this piece of code:



      if [ -f /etc/bashrc ]; then
      . /etc/bashrc
      fi


      I'm not sure what the -f means exactly.







      share|improve this question











      Trying to understand this piece of code:



      if [ -f /etc/bashrc ]; then
      . /etc/bashrc
      fi


      I'm not sure what the -f means exactly.









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jul 4 at 21:56









      Eric Hodgins

      192




      192




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          The relevant man page to check for this is that of the shell itself, bash, because -f is functionality that the shell provides, it's a bash built-in.



          On my system (CentOS 7), the fine man page covers it. The grep may not give the same results on other distributions. Nevertheless, if you run man bash and then search for '-f' it should give the results you require.



          $ man bash | grep -A1 '-f file$'
          -f file
          True if file exists and is a regular file.
          $





          share|improve this answer






























            up vote
            7
            down vote













            In short, the piece of code will source /etc/bashrc file if it exists, and the existence is verified by [ command to which -f is an operator/parameter.



            if...then...else...fi statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:



            if ping -c 4 google.com; then
            echo "We have a connection!"
            fi


            The command, in your case, is [ which is also known as test command. So it'd be perfectly valid to do



            if test -f /etc/bashrc; then
            . /etc/bashrc
            fi


            The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc is in fact a directory or missing, test should return non-zero exit status to signal failure



            This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.



            On a side note, the /etc/bashrc seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc, which is intended as system-wide rc-file for bash, so one would expect that to be used.



            See also:



            • How to conditionally do something if a command succeeded or failed

            • How exactly does “/bin/[” work?

            • What is the difference between the Bash operators [[ vs [ vs ( vs ((?





            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%2f453502%2fwhat-does-f-mean-in-an-if-statement-in-a-bash-script%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
              6
              down vote



              accepted










              The relevant man page to check for this is that of the shell itself, bash, because -f is functionality that the shell provides, it's a bash built-in.



              On my system (CentOS 7), the fine man page covers it. The grep may not give the same results on other distributions. Nevertheless, if you run man bash and then search for '-f' it should give the results you require.



              $ man bash | grep -A1 '-f file$'
              -f file
              True if file exists and is a regular file.
              $





              share|improve this answer



























                up vote
                6
                down vote



                accepted










                The relevant man page to check for this is that of the shell itself, bash, because -f is functionality that the shell provides, it's a bash built-in.



                On my system (CentOS 7), the fine man page covers it. The grep may not give the same results on other distributions. Nevertheless, if you run man bash and then search for '-f' it should give the results you require.



                $ man bash | grep -A1 '-f file$'
                -f file
                True if file exists and is a regular file.
                $





                share|improve this answer

























                  up vote
                  6
                  down vote



                  accepted







                  up vote
                  6
                  down vote



                  accepted






                  The relevant man page to check for this is that of the shell itself, bash, because -f is functionality that the shell provides, it's a bash built-in.



                  On my system (CentOS 7), the fine man page covers it. The grep may not give the same results on other distributions. Nevertheless, if you run man bash and then search for '-f' it should give the results you require.



                  $ man bash | grep -A1 '-f file$'
                  -f file
                  True if file exists and is a regular file.
                  $





                  share|improve this answer















                  The relevant man page to check for this is that of the shell itself, bash, because -f is functionality that the shell provides, it's a bash built-in.



                  On my system (CentOS 7), the fine man page covers it. The grep may not give the same results on other distributions. Nevertheless, if you run man bash and then search for '-f' it should give the results you require.



                  $ man bash | grep -A1 '-f file$'
                  -f file
                  True if file exists and is a regular file.
                  $






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jul 6 at 13:03


























                  answered Jul 4 at 22:11









                  steve

                  12k22047




                  12k22047






















                      up vote
                      7
                      down vote













                      In short, the piece of code will source /etc/bashrc file if it exists, and the existence is verified by [ command to which -f is an operator/parameter.



                      if...then...else...fi statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:



                      if ping -c 4 google.com; then
                      echo "We have a connection!"
                      fi


                      The command, in your case, is [ which is also known as test command. So it'd be perfectly valid to do



                      if test -f /etc/bashrc; then
                      . /etc/bashrc
                      fi


                      The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc is in fact a directory or missing, test should return non-zero exit status to signal failure



                      This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.



                      On a side note, the /etc/bashrc seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc, which is intended as system-wide rc-file for bash, so one would expect that to be used.



                      See also:



                      • How to conditionally do something if a command succeeded or failed

                      • How exactly does “/bin/[” work?

                      • What is the difference between the Bash operators [[ vs [ vs ( vs ((?





                      share|improve this answer



























                        up vote
                        7
                        down vote













                        In short, the piece of code will source /etc/bashrc file if it exists, and the existence is verified by [ command to which -f is an operator/parameter.



                        if...then...else...fi statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:



                        if ping -c 4 google.com; then
                        echo "We have a connection!"
                        fi


                        The command, in your case, is [ which is also known as test command. So it'd be perfectly valid to do



                        if test -f /etc/bashrc; then
                        . /etc/bashrc
                        fi


                        The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc is in fact a directory or missing, test should return non-zero exit status to signal failure



                        This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.



                        On a side note, the /etc/bashrc seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc, which is intended as system-wide rc-file for bash, so one would expect that to be used.



                        See also:



                        • How to conditionally do something if a command succeeded or failed

                        • How exactly does “/bin/[” work?

                        • What is the difference between the Bash operators [[ vs [ vs ( vs ((?





                        share|improve this answer

























                          up vote
                          7
                          down vote










                          up vote
                          7
                          down vote









                          In short, the piece of code will source /etc/bashrc file if it exists, and the existence is verified by [ command to which -f is an operator/parameter.



                          if...then...else...fi statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:



                          if ping -c 4 google.com; then
                          echo "We have a connection!"
                          fi


                          The command, in your case, is [ which is also known as test command. So it'd be perfectly valid to do



                          if test -f /etc/bashrc; then
                          . /etc/bashrc
                          fi


                          The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc is in fact a directory or missing, test should return non-zero exit status to signal failure



                          This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.



                          On a side note, the /etc/bashrc seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc, which is intended as system-wide rc-file for bash, so one would expect that to be used.



                          See also:



                          • How to conditionally do something if a command succeeded or failed

                          • How exactly does “/bin/[” work?

                          • What is the difference between the Bash operators [[ vs [ vs ( vs ((?





                          share|improve this answer















                          In short, the piece of code will source /etc/bashrc file if it exists, and the existence is verified by [ command to which -f is an operator/parameter.



                          if...then...else...fi statement in shell scripting evaluates exit status of commands - 0 on success. So it's proper to do something like this:



                          if ping -c 4 google.com; then
                          echo "We have a connection!"
                          fi


                          The command, in your case, is [ which is also known as test command. So it'd be perfectly valid to do



                          if test -f /etc/bashrc; then
                          . /etc/bashrc
                          fi


                          The -f flag verifies two things: the provided path exists and is a regular file. If /etc/bashrc is in fact a directory or missing, test should return non-zero exit status to signal failure



                          This command originally was a separate command, that is not part of shell's built-in commands. Nowadays, most Bourne-like shells have it as built-in, and that's what shell will use.



                          On a side note, the /etc/bashrc seems like unnecessary extra file that your admin or original author of the code snippet is using. There exists /etc/bash.bashrc, which is intended as system-wide rc-file for bash, so one would expect that to be used.



                          See also:



                          • How to conditionally do something if a command succeeded or failed

                          • How exactly does “/bin/[” work?

                          • What is the difference between the Bash operators [[ vs [ vs ( vs ((?






                          share|improve this answer















                          share|improve this answer



                          share|improve this answer








                          edited Jul 5 at 8:49


























                          answered Jul 4 at 23:18









                          Sergiy Kolodyazhnyy

                          7,53811545




                          7,53811545






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453502%2fwhat-does-f-mean-in-an-if-statement-in-a-bash-script%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?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay