watch aliased cmd doesn't work





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















When I do



$ watch kubectl get pods


it works fine but on doing:



$ alias p0="kubectl get pods"
$ watch p0


gives an error:
Every 2.0s: p0

sh: p0: command not found



It looks like watch is starting a subshell and the aliases in my current shell aren't visible to the subshell. I do have



$ shopt -s expand_aliases


at the very top of my .bashrc but it isn't helping.



Trying this with bash version 3.2.57 on Mac OS Mojave.



Update:
Tried a few more things:



$  watch -n 0.1 "source ~/.bashrc; shopt; alias p0; p0"


still doesn't work.



cdable_vars     off
cdspell off
checkhash off
checkwinsize off
cmdhist on
compat31 off
dotglob off
execfail off
expand_aliases on
extdebug off
extglob off
extquote on
failglob off
force_fignore on
gnu_errfmt off
histappend on
histreedit off
histverify off
hostcomplete on
huponexit off
interactive_comments on
lithist off
login_shell off
mailwarn off
no_empty_cmd_completion off
nocaseglob off
nocasematch off
nullglob off
progcomp on
promptvars on
restricted_shell off
shift_verbose off
sourcepath on
xpg_echo on
p0='kubectl get pods' <--- HERE'S THE ALIAS
sh: p0: command not found <--- STILL DOESN'T EXECUTE IT.









share|improve this question































    0















    When I do



    $ watch kubectl get pods


    it works fine but on doing:



    $ alias p0="kubectl get pods"
    $ watch p0


    gives an error:
    Every 2.0s: p0

    sh: p0: command not found



    It looks like watch is starting a subshell and the aliases in my current shell aren't visible to the subshell. I do have



    $ shopt -s expand_aliases


    at the very top of my .bashrc but it isn't helping.



    Trying this with bash version 3.2.57 on Mac OS Mojave.



    Update:
    Tried a few more things:



    $  watch -n 0.1 "source ~/.bashrc; shopt; alias p0; p0"


    still doesn't work.



    cdable_vars     off
    cdspell off
    checkhash off
    checkwinsize off
    cmdhist on
    compat31 off
    dotglob off
    execfail off
    expand_aliases on
    extdebug off
    extglob off
    extquote on
    failglob off
    force_fignore on
    gnu_errfmt off
    histappend on
    histreedit off
    histverify off
    hostcomplete on
    huponexit off
    interactive_comments on
    lithist off
    login_shell off
    mailwarn off
    no_empty_cmd_completion off
    nocaseglob off
    nocasematch off
    nullglob off
    progcomp on
    promptvars on
    restricted_shell off
    shift_verbose off
    sourcepath on
    xpg_echo on
    p0='kubectl get pods' <--- HERE'S THE ALIAS
    sh: p0: command not found <--- STILL DOESN'T EXECUTE IT.









    share|improve this question



























      0












      0








      0








      When I do



      $ watch kubectl get pods


      it works fine but on doing:



      $ alias p0="kubectl get pods"
      $ watch p0


      gives an error:
      Every 2.0s: p0

      sh: p0: command not found



      It looks like watch is starting a subshell and the aliases in my current shell aren't visible to the subshell. I do have



      $ shopt -s expand_aliases


      at the very top of my .bashrc but it isn't helping.



      Trying this with bash version 3.2.57 on Mac OS Mojave.



      Update:
      Tried a few more things:



      $  watch -n 0.1 "source ~/.bashrc; shopt; alias p0; p0"


      still doesn't work.



      cdable_vars     off
      cdspell off
      checkhash off
      checkwinsize off
      cmdhist on
      compat31 off
      dotglob off
      execfail off
      expand_aliases on
      extdebug off
      extglob off
      extquote on
      failglob off
      force_fignore on
      gnu_errfmt off
      histappend on
      histreedit off
      histverify off
      hostcomplete on
      huponexit off
      interactive_comments on
      lithist off
      login_shell off
      mailwarn off
      no_empty_cmd_completion off
      nocaseglob off
      nocasematch off
      nullglob off
      progcomp on
      promptvars on
      restricted_shell off
      shift_verbose off
      sourcepath on
      xpg_echo on
      p0='kubectl get pods' <--- HERE'S THE ALIAS
      sh: p0: command not found <--- STILL DOESN'T EXECUTE IT.









      share|improve this question
















      When I do



      $ watch kubectl get pods


      it works fine but on doing:



      $ alias p0="kubectl get pods"
      $ watch p0


      gives an error:
      Every 2.0s: p0

      sh: p0: command not found



      It looks like watch is starting a subshell and the aliases in my current shell aren't visible to the subshell. I do have



      $ shopt -s expand_aliases


      at the very top of my .bashrc but it isn't helping.



      Trying this with bash version 3.2.57 on Mac OS Mojave.



      Update:
      Tried a few more things:



      $  watch -n 0.1 "source ~/.bashrc; shopt; alias p0; p0"


      still doesn't work.



      cdable_vars     off
      cdspell off
      checkhash off
      checkwinsize off
      cmdhist on
      compat31 off
      dotglob off
      execfail off
      expand_aliases on
      extdebug off
      extglob off
      extquote on
      failglob off
      force_fignore on
      gnu_errfmt off
      histappend on
      histreedit off
      histverify off
      hostcomplete on
      huponexit off
      interactive_comments on
      lithist off
      login_shell off
      mailwarn off
      no_empty_cmd_completion off
      nocaseglob off
      nocasematch off
      nullglob off
      progcomp on
      promptvars on
      restricted_shell off
      shift_verbose off
      sourcepath on
      xpg_echo on
      p0='kubectl get pods' <--- HERE'S THE ALIAS
      sh: p0: command not found <--- STILL DOESN'T EXECUTE IT.






      bash bash-scripting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 12 at 6:31







      user674669

















      asked Mar 12 at 5:58









      user674669user674669

      193217




      193217






















          2 Answers
          2






          active

          oldest

          votes


















          2














          Aliases are not inherited by subshells. Functions may be exported but while this approach makes Bash inherit exported function(s) from Bash, watch will interfere, mainly because it spawns sh, not bash.



          You have at least three options:




          1. Make p0 a script somewhere in $PATH, so watch can run it like any other executable. If done right, this will be very robust.



          2. Define a special alias for watch:



            alias watch='watch '


            Then this happens:




            If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.




            source



            So watch p0 will work (but watch -n 4 p0 won't).




          3. Define an alias (or function, or script, whatever) for the whole command:



            alias wp0='watch kubectl get pods'


            and type just wp0.








          share|improve this answer


























          • Great answer @Kamil. Reg. your first answer, I could make it a script. However, I have hundreds of aliases collected over the years and making them individual scripts seems like a lot of work.

            – user674669
            Mar 12 at 6:40






          • 1





            +1 on yr 2nd point. Been using aliases for yrs w/o ever having come across that nugget.

            – Cbhihe
            Mar 12 at 7:18



















          1














          function watcha() {
          a=$(alias $1) # extract the cmd from the alias

          #remove = sign and first/last ' before executing thru watch
          watch $(echo $a | awk -F= '{print $2}'|sed 's/.$//'|sed 's/^.//')
          }


          Now when I do



          $ watcha p0


          it does work as expected.






          share|improve this answer
























          • alias watcha='watch ' seems more robust and so much simpler.

            – Kamil Maciorowski
            Mar 13 at 16:37












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1413282%2fwatch-aliased-cmd-doesnt-work%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Aliases are not inherited by subshells. Functions may be exported but while this approach makes Bash inherit exported function(s) from Bash, watch will interfere, mainly because it spawns sh, not bash.



          You have at least three options:




          1. Make p0 a script somewhere in $PATH, so watch can run it like any other executable. If done right, this will be very robust.



          2. Define a special alias for watch:



            alias watch='watch '


            Then this happens:




            If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.




            source



            So watch p0 will work (but watch -n 4 p0 won't).




          3. Define an alias (or function, or script, whatever) for the whole command:



            alias wp0='watch kubectl get pods'


            and type just wp0.








          share|improve this answer


























          • Great answer @Kamil. Reg. your first answer, I could make it a script. However, I have hundreds of aliases collected over the years and making them individual scripts seems like a lot of work.

            – user674669
            Mar 12 at 6:40






          • 1





            +1 on yr 2nd point. Been using aliases for yrs w/o ever having come across that nugget.

            – Cbhihe
            Mar 12 at 7:18
















          2














          Aliases are not inherited by subshells. Functions may be exported but while this approach makes Bash inherit exported function(s) from Bash, watch will interfere, mainly because it spawns sh, not bash.



          You have at least three options:




          1. Make p0 a script somewhere in $PATH, so watch can run it like any other executable. If done right, this will be very robust.



          2. Define a special alias for watch:



            alias watch='watch '


            Then this happens:




            If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.




            source



            So watch p0 will work (but watch -n 4 p0 won't).




          3. Define an alias (or function, or script, whatever) for the whole command:



            alias wp0='watch kubectl get pods'


            and type just wp0.








          share|improve this answer


























          • Great answer @Kamil. Reg. your first answer, I could make it a script. However, I have hundreds of aliases collected over the years and making them individual scripts seems like a lot of work.

            – user674669
            Mar 12 at 6:40






          • 1





            +1 on yr 2nd point. Been using aliases for yrs w/o ever having come across that nugget.

            – Cbhihe
            Mar 12 at 7:18














          2












          2








          2







          Aliases are not inherited by subshells. Functions may be exported but while this approach makes Bash inherit exported function(s) from Bash, watch will interfere, mainly because it spawns sh, not bash.



          You have at least three options:




          1. Make p0 a script somewhere in $PATH, so watch can run it like any other executable. If done right, this will be very robust.



          2. Define a special alias for watch:



            alias watch='watch '


            Then this happens:




            If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.




            source



            So watch p0 will work (but watch -n 4 p0 won't).




          3. Define an alias (or function, or script, whatever) for the whole command:



            alias wp0='watch kubectl get pods'


            and type just wp0.








          share|improve this answer















          Aliases are not inherited by subshells. Functions may be exported but while this approach makes Bash inherit exported function(s) from Bash, watch will interfere, mainly because it spawns sh, not bash.



          You have at least three options:




          1. Make p0 a script somewhere in $PATH, so watch can run it like any other executable. If done right, this will be very robust.



          2. Define a special alias for watch:



            alias watch='watch '


            Then this happens:




            If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.




            source



            So watch p0 will work (but watch -n 4 p0 won't).




          3. Define an alias (or function, or script, whatever) for the whole command:



            alias wp0='watch kubectl get pods'


            and type just wp0.









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 12 at 6:55

























          answered Mar 12 at 6:31









          Kamil MaciorowskiKamil Maciorowski

          29.2k156288




          29.2k156288













          • Great answer @Kamil. Reg. your first answer, I could make it a script. However, I have hundreds of aliases collected over the years and making them individual scripts seems like a lot of work.

            – user674669
            Mar 12 at 6:40






          • 1





            +1 on yr 2nd point. Been using aliases for yrs w/o ever having come across that nugget.

            – Cbhihe
            Mar 12 at 7:18



















          • Great answer @Kamil. Reg. your first answer, I could make it a script. However, I have hundreds of aliases collected over the years and making them individual scripts seems like a lot of work.

            – user674669
            Mar 12 at 6:40






          • 1





            +1 on yr 2nd point. Been using aliases for yrs w/o ever having come across that nugget.

            – Cbhihe
            Mar 12 at 7:18

















          Great answer @Kamil. Reg. your first answer, I could make it a script. However, I have hundreds of aliases collected over the years and making them individual scripts seems like a lot of work.

          – user674669
          Mar 12 at 6:40





          Great answer @Kamil. Reg. your first answer, I could make it a script. However, I have hundreds of aliases collected over the years and making them individual scripts seems like a lot of work.

          – user674669
          Mar 12 at 6:40




          1




          1





          +1 on yr 2nd point. Been using aliases for yrs w/o ever having come across that nugget.

          – Cbhihe
          Mar 12 at 7:18





          +1 on yr 2nd point. Been using aliases for yrs w/o ever having come across that nugget.

          – Cbhihe
          Mar 12 at 7:18













          1














          function watcha() {
          a=$(alias $1) # extract the cmd from the alias

          #remove = sign and first/last ' before executing thru watch
          watch $(echo $a | awk -F= '{print $2}'|sed 's/.$//'|sed 's/^.//')
          }


          Now when I do



          $ watcha p0


          it does work as expected.






          share|improve this answer
























          • alias watcha='watch ' seems more robust and so much simpler.

            – Kamil Maciorowski
            Mar 13 at 16:37
















          1














          function watcha() {
          a=$(alias $1) # extract the cmd from the alias

          #remove = sign and first/last ' before executing thru watch
          watch $(echo $a | awk -F= '{print $2}'|sed 's/.$//'|sed 's/^.//')
          }


          Now when I do



          $ watcha p0


          it does work as expected.






          share|improve this answer
























          • alias watcha='watch ' seems more robust and so much simpler.

            – Kamil Maciorowski
            Mar 13 at 16:37














          1












          1








          1







          function watcha() {
          a=$(alias $1) # extract the cmd from the alias

          #remove = sign and first/last ' before executing thru watch
          watch $(echo $a | awk -F= '{print $2}'|sed 's/.$//'|sed 's/^.//')
          }


          Now when I do



          $ watcha p0


          it does work as expected.






          share|improve this answer













          function watcha() {
          a=$(alias $1) # extract the cmd from the alias

          #remove = sign and first/last ' before executing thru watch
          watch $(echo $a | awk -F= '{print $2}'|sed 's/.$//'|sed 's/^.//')
          }


          Now when I do



          $ watcha p0


          it does work as expected.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 12 at 7:02









          user674669user674669

          193217




          193217













          • alias watcha='watch ' seems more robust and so much simpler.

            – Kamil Maciorowski
            Mar 13 at 16:37



















          • alias watcha='watch ' seems more robust and so much simpler.

            – Kamil Maciorowski
            Mar 13 at 16:37

















          alias watcha='watch ' seems more robust and so much simpler.

          – Kamil Maciorowski
          Mar 13 at 16:37





          alias watcha='watch ' seems more robust and so much simpler.

          – Kamil Maciorowski
          Mar 13 at 16:37


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1413282%2fwatch-aliased-cmd-doesnt-work%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How do I know what Microsoft account the skydrive app is syncing to?

          When does type information flow backwards in C++?

          Grease: Live!