Define function in fish, use it with watch

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











up vote
0
down vote

favorite












I want to define a function, and call that function every n seconds. As an example:



function h
echo hello
end


Calling h works:



david@f5 ~> h
hello


But when using watch, it doesn't...



watch -n 60 "h"


...and I get:



Every 60.0s: h f5: Wed Oct 10 21:04:15 2018

sh: 1: h: not found


How can I run watch in fish, with the function I've just defined?










share|improve this question



























    up vote
    0
    down vote

    favorite












    I want to define a function, and call that function every n seconds. As an example:



    function h
    echo hello
    end


    Calling h works:



    david@f5 ~> h
    hello


    But when using watch, it doesn't...



    watch -n 60 "h"


    ...and I get:



    Every 60.0s: h f5: Wed Oct 10 21:04:15 2018

    sh: 1: h: not found


    How can I run watch in fish, with the function I've just defined?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to define a function, and call that function every n seconds. As an example:



      function h
      echo hello
      end


      Calling h works:



      david@f5 ~> h
      hello


      But when using watch, it doesn't...



      watch -n 60 "h"


      ...and I get:



      Every 60.0s: h f5: Wed Oct 10 21:04:15 2018

      sh: 1: h: not found


      How can I run watch in fish, with the function I've just defined?










      share|improve this question















      I want to define a function, and call that function every n seconds. As an example:



      function h
      echo hello
      end


      Calling h works:



      david@f5 ~> h
      hello


      But when using watch, it doesn't...



      watch -n 60 "h"


      ...and I get:



      Every 60.0s: h f5: Wed Oct 10 21:04:15 2018

      sh: 1: h: not found


      How can I run watch in fish, with the function I've just defined?







      function fish watch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 18 mins ago









      Jeff Schaller

      33.8k851113




      33.8k851113










      asked 37 mins ago









      user258532

      1193




      1193




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          There is no easy way. By default watch uses /bin/sh to run commands but takes -x:



           -x, --exec
          Pass command to exec(2) instead of sh -c which reduces the need to use extra quoting to get the
          desired effect.


          However, nothing will not work with fish because h function is not
          exported to environment:



          $ watch -n 5 --exec fish -c h
          Every 5.0s: fish -c h comp: Wed Oct 10 21:30:14 2018

          fish: Unknown command 'h'
          fish:
          h
          ^


          In bash you could export a variable to environment with export -f
          and use it inside watch like this:



          $ h1 () 
          > echo hi
          >
          $ type h1
          h1 is a function
          h1 ()

          echo hi

          $ export -f h1
          $ watch -n 60 bash -c h1
          Every 60.0s: bash -c h1 comp: Wed Oct 10 21:29:22 2018

          hi


          If you use fish you can create a wrapper script and call it with watch:



          $ cat stuff.sh
          #!/usr/bin/env fish

          function h
          date
          end

          h

          $ watch -n5 ./stuff.sh


          Also note that fish has . and source so you can define function
          in another file and be able to re-use it in other scripts like that:



          $ cat function
          function h
          echo hi
          end
          $ cat call.sh
          #!/usr/bin/env fish

          . function

          h
          $ watch ./call.sh




          share




















            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%2f474628%2fdefine-function-in-fish-use-it-with-watch%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            There is no easy way. By default watch uses /bin/sh to run commands but takes -x:



             -x, --exec
            Pass command to exec(2) instead of sh -c which reduces the need to use extra quoting to get the
            desired effect.


            However, nothing will not work with fish because h function is not
            exported to environment:



            $ watch -n 5 --exec fish -c h
            Every 5.0s: fish -c h comp: Wed Oct 10 21:30:14 2018

            fish: Unknown command 'h'
            fish:
            h
            ^


            In bash you could export a variable to environment with export -f
            and use it inside watch like this:



            $ h1 () 
            > echo hi
            >
            $ type h1
            h1 is a function
            h1 ()

            echo hi

            $ export -f h1
            $ watch -n 60 bash -c h1
            Every 60.0s: bash -c h1 comp: Wed Oct 10 21:29:22 2018

            hi


            If you use fish you can create a wrapper script and call it with watch:



            $ cat stuff.sh
            #!/usr/bin/env fish

            function h
            date
            end

            h

            $ watch -n5 ./stuff.sh


            Also note that fish has . and source so you can define function
            in another file and be able to re-use it in other scripts like that:



            $ cat function
            function h
            echo hi
            end
            $ cat call.sh
            #!/usr/bin/env fish

            . function

            h
            $ watch ./call.sh




            share
























              up vote
              0
              down vote













              There is no easy way. By default watch uses /bin/sh to run commands but takes -x:



               -x, --exec
              Pass command to exec(2) instead of sh -c which reduces the need to use extra quoting to get the
              desired effect.


              However, nothing will not work with fish because h function is not
              exported to environment:



              $ watch -n 5 --exec fish -c h
              Every 5.0s: fish -c h comp: Wed Oct 10 21:30:14 2018

              fish: Unknown command 'h'
              fish:
              h
              ^


              In bash you could export a variable to environment with export -f
              and use it inside watch like this:



              $ h1 () 
              > echo hi
              >
              $ type h1
              h1 is a function
              h1 ()

              echo hi

              $ export -f h1
              $ watch -n 60 bash -c h1
              Every 60.0s: bash -c h1 comp: Wed Oct 10 21:29:22 2018

              hi


              If you use fish you can create a wrapper script and call it with watch:



              $ cat stuff.sh
              #!/usr/bin/env fish

              function h
              date
              end

              h

              $ watch -n5 ./stuff.sh


              Also note that fish has . and source so you can define function
              in another file and be able to re-use it in other scripts like that:



              $ cat function
              function h
              echo hi
              end
              $ cat call.sh
              #!/usr/bin/env fish

              . function

              h
              $ watch ./call.sh




              share






















                up vote
                0
                down vote










                up vote
                0
                down vote









                There is no easy way. By default watch uses /bin/sh to run commands but takes -x:



                 -x, --exec
                Pass command to exec(2) instead of sh -c which reduces the need to use extra quoting to get the
                desired effect.


                However, nothing will not work with fish because h function is not
                exported to environment:



                $ watch -n 5 --exec fish -c h
                Every 5.0s: fish -c h comp: Wed Oct 10 21:30:14 2018

                fish: Unknown command 'h'
                fish:
                h
                ^


                In bash you could export a variable to environment with export -f
                and use it inside watch like this:



                $ h1 () 
                > echo hi
                >
                $ type h1
                h1 is a function
                h1 ()

                echo hi

                $ export -f h1
                $ watch -n 60 bash -c h1
                Every 60.0s: bash -c h1 comp: Wed Oct 10 21:29:22 2018

                hi


                If you use fish you can create a wrapper script and call it with watch:



                $ cat stuff.sh
                #!/usr/bin/env fish

                function h
                date
                end

                h

                $ watch -n5 ./stuff.sh


                Also note that fish has . and source so you can define function
                in another file and be able to re-use it in other scripts like that:



                $ cat function
                function h
                echo hi
                end
                $ cat call.sh
                #!/usr/bin/env fish

                . function

                h
                $ watch ./call.sh




                share












                There is no easy way. By default watch uses /bin/sh to run commands but takes -x:



                 -x, --exec
                Pass command to exec(2) instead of sh -c which reduces the need to use extra quoting to get the
                desired effect.


                However, nothing will not work with fish because h function is not
                exported to environment:



                $ watch -n 5 --exec fish -c h
                Every 5.0s: fish -c h comp: Wed Oct 10 21:30:14 2018

                fish: Unknown command 'h'
                fish:
                h
                ^


                In bash you could export a variable to environment with export -f
                and use it inside watch like this:



                $ h1 () 
                > echo hi
                >
                $ type h1
                h1 is a function
                h1 ()

                echo hi

                $ export -f h1
                $ watch -n 60 bash -c h1
                Every 60.0s: bash -c h1 comp: Wed Oct 10 21:29:22 2018

                hi


                If you use fish you can create a wrapper script and call it with watch:



                $ cat stuff.sh
                #!/usr/bin/env fish

                function h
                date
                end

                h

                $ watch -n5 ./stuff.sh


                Also note that fish has . and source so you can define function
                in another file and be able to re-use it in other scripts like that:



                $ cat function
                function h
                echo hi
                end
                $ cat call.sh
                #!/usr/bin/env fish

                . function

                h
                $ watch ./call.sh





                share











                share


                share










                answered 6 mins ago









                Arkadiusz Drabczyk

                7,39521633




                7,39521633



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474628%2fdefine-function-in-fish-use-it-with-watch%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