Skip to main content
edited body
Source Link
Arkadiusz Drabczyk
  • 25.7k
  • 5
  • 55
  • 70

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

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

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

added 26 characters in body
Source Link
Arkadiusz Drabczyk
  • 25.7k
  • 5
  • 55
  • 70
   -x, --exec
          Pass  command  to  exec(2)  instead  of  sh -c which reduces the need to use extra quoting to get the  
          the need to use extra quoting to get the desired effect.
   -x, --exec
          Pass  command  to  exec(2)  instead  of  sh -c which reduces the need to use extra quoting to get the
          desired effect.
   -x, --exec
          Pass  command  to  exec(2)  instead  of  sh -c which reduces           
          the need to use extra quoting to get the desired effect.
Source Link
Arkadiusz Drabczyk
  • 25.7k
  • 5
  • 55
  • 70

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