Some commands not working when executed via ssh while redirecting output locally

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











up vote
1
down vote

favorite












I can run the following two commands for example and get the output locally:



ssh user@remote.ip ls > testhistory.txt 
ssh user@remote.ip "cat .bash_history" > testhistory.txt


But if I run the following command, the local output is always empty:



ssh user@remote.ip history > testhistory.txt


If I ssh to the remote destination and then run the history command, i get the expected output.



Why does the history command not output results when run inline with ssh but the ls command works normally? What do i need to change to make the history command output results to local file the way I did the ls command without having to cat the .bash_history file?










share|improve this question



























    up vote
    1
    down vote

    favorite












    I can run the following two commands for example and get the output locally:



    ssh user@remote.ip ls > testhistory.txt 
    ssh user@remote.ip "cat .bash_history" > testhistory.txt


    But if I run the following command, the local output is always empty:



    ssh user@remote.ip history > testhistory.txt


    If I ssh to the remote destination and then run the history command, i get the expected output.



    Why does the history command not output results when run inline with ssh but the ls command works normally? What do i need to change to make the history command output results to local file the way I did the ls command without having to cat the .bash_history file?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I can run the following two commands for example and get the output locally:



      ssh user@remote.ip ls > testhistory.txt 
      ssh user@remote.ip "cat .bash_history" > testhistory.txt


      But if I run the following command, the local output is always empty:



      ssh user@remote.ip history > testhistory.txt


      If I ssh to the remote destination and then run the history command, i get the expected output.



      Why does the history command not output results when run inline with ssh but the ls command works normally? What do i need to change to make the history command output results to local file the way I did the ls command without having to cat the .bash_history file?










      share|improve this question















      I can run the following two commands for example and get the output locally:



      ssh user@remote.ip ls > testhistory.txt 
      ssh user@remote.ip "cat .bash_history" > testhistory.txt


      But if I run the following command, the local output is always empty:



      ssh user@remote.ip history > testhistory.txt


      If I ssh to the remote destination and then run the history command, i get the expected output.



      Why does the history command not output results when run inline with ssh but the ls command works normally? What do i need to change to make the history command output results to local file the way I did the ls command without having to cat the .bash_history file?







      bash ssh command-history






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 10 at 23:01









      Jeff Schaller

      33.1k849111




      33.1k849111










      asked Sep 10 at 22:56









      onlineoffline

      153




      153




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          history is an internal command to bash, so you don't want to run the executable history, you want to run the executable bash. ls works because is an executable on its own, usually at /bin/ls.



          Besides that, by default bash disables the history in non-interactive shells, as is the case when you run a remote command.



          You can create a shell script in the remote machine to enable it and run history, example:



          #!/usr/bin/bash
          HISTFILE=~/.bash_history
          set -o history
          history


          Or, if you really want to do all that from the ssh call, you can do:



          ssh user@remote.ip 'echo -e "HISTFILE=~/.bash_historynset -o historynhistory" | bash'


          Note that it also doesn't take into account the HISTTIMEFORMAT variable, if you use it in the remote machine, so plan for that.



          References: History command inside bash script






          share|improve this answer






















          • what would a working command look like? ssh user@remote.ip "bash history" > testhistory.txt doesn't work
            – onlineoffline
            Sep 10 at 23:24






          • 1




            ssh with additional non-option arguments passes them to the remote shell (all as one -c) so builtins DO work (try ssh u@h declare -p); noninteractive -> disable history is the only issue. And you don't need the remote shell to run yet another, just ssh u@h 'HISTFILE=~/.bash_history; set -o history; history' -- or even quote only the semicolons: ssh u@h HISTFILE=~/.bash_history';'set -o history';'history but that looks really odd
            – dave_thompson_085
            Sep 11 at 3:17











          • Is there a way to make all variables enabled or find out what's disabled when in non-interactive shell?
            – onlineoffline
            Sep 11 at 19:10










          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%2f468130%2fsome-commands-not-working-when-executed-via-ssh-while-redirecting-output-locally%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
          2
          down vote



          accepted










          history is an internal command to bash, so you don't want to run the executable history, you want to run the executable bash. ls works because is an executable on its own, usually at /bin/ls.



          Besides that, by default bash disables the history in non-interactive shells, as is the case when you run a remote command.



          You can create a shell script in the remote machine to enable it and run history, example:



          #!/usr/bin/bash
          HISTFILE=~/.bash_history
          set -o history
          history


          Or, if you really want to do all that from the ssh call, you can do:



          ssh user@remote.ip 'echo -e "HISTFILE=~/.bash_historynset -o historynhistory" | bash'


          Note that it also doesn't take into account the HISTTIMEFORMAT variable, if you use it in the remote machine, so plan for that.



          References: History command inside bash script






          share|improve this answer






















          • what would a working command look like? ssh user@remote.ip "bash history" > testhistory.txt doesn't work
            – onlineoffline
            Sep 10 at 23:24






          • 1




            ssh with additional non-option arguments passes them to the remote shell (all as one -c) so builtins DO work (try ssh u@h declare -p); noninteractive -> disable history is the only issue. And you don't need the remote shell to run yet another, just ssh u@h 'HISTFILE=~/.bash_history; set -o history; history' -- or even quote only the semicolons: ssh u@h HISTFILE=~/.bash_history';'set -o history';'history but that looks really odd
            – dave_thompson_085
            Sep 11 at 3:17











          • Is there a way to make all variables enabled or find out what's disabled when in non-interactive shell?
            – onlineoffline
            Sep 11 at 19:10














          up vote
          2
          down vote



          accepted










          history is an internal command to bash, so you don't want to run the executable history, you want to run the executable bash. ls works because is an executable on its own, usually at /bin/ls.



          Besides that, by default bash disables the history in non-interactive shells, as is the case when you run a remote command.



          You can create a shell script in the remote machine to enable it and run history, example:



          #!/usr/bin/bash
          HISTFILE=~/.bash_history
          set -o history
          history


          Or, if you really want to do all that from the ssh call, you can do:



          ssh user@remote.ip 'echo -e "HISTFILE=~/.bash_historynset -o historynhistory" | bash'


          Note that it also doesn't take into account the HISTTIMEFORMAT variable, if you use it in the remote machine, so plan for that.



          References: History command inside bash script






          share|improve this answer






















          • what would a working command look like? ssh user@remote.ip "bash history" > testhistory.txt doesn't work
            – onlineoffline
            Sep 10 at 23:24






          • 1




            ssh with additional non-option arguments passes them to the remote shell (all as one -c) so builtins DO work (try ssh u@h declare -p); noninteractive -> disable history is the only issue. And you don't need the remote shell to run yet another, just ssh u@h 'HISTFILE=~/.bash_history; set -o history; history' -- or even quote only the semicolons: ssh u@h HISTFILE=~/.bash_history';'set -o history';'history but that looks really odd
            – dave_thompson_085
            Sep 11 at 3:17











          • Is there a way to make all variables enabled or find out what's disabled when in non-interactive shell?
            – onlineoffline
            Sep 11 at 19:10












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          history is an internal command to bash, so you don't want to run the executable history, you want to run the executable bash. ls works because is an executable on its own, usually at /bin/ls.



          Besides that, by default bash disables the history in non-interactive shells, as is the case when you run a remote command.



          You can create a shell script in the remote machine to enable it and run history, example:



          #!/usr/bin/bash
          HISTFILE=~/.bash_history
          set -o history
          history


          Or, if you really want to do all that from the ssh call, you can do:



          ssh user@remote.ip 'echo -e "HISTFILE=~/.bash_historynset -o historynhistory" | bash'


          Note that it also doesn't take into account the HISTTIMEFORMAT variable, if you use it in the remote machine, so plan for that.



          References: History command inside bash script






          share|improve this answer














          history is an internal command to bash, so you don't want to run the executable history, you want to run the executable bash. ls works because is an executable on its own, usually at /bin/ls.



          Besides that, by default bash disables the history in non-interactive shells, as is the case when you run a remote command.



          You can create a shell script in the remote machine to enable it and run history, example:



          #!/usr/bin/bash
          HISTFILE=~/.bash_history
          set -o history
          history


          Or, if you really want to do all that from the ssh call, you can do:



          ssh user@remote.ip 'echo -e "HISTFILE=~/.bash_historynset -o historynhistory" | bash'


          Note that it also doesn't take into account the HISTTIMEFORMAT variable, if you use it in the remote machine, so plan for that.



          References: History command inside bash script







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 10 at 23:31

























          answered Sep 10 at 23:16









          msb

          1,15079




          1,15079











          • what would a working command look like? ssh user@remote.ip "bash history" > testhistory.txt doesn't work
            – onlineoffline
            Sep 10 at 23:24






          • 1




            ssh with additional non-option arguments passes them to the remote shell (all as one -c) so builtins DO work (try ssh u@h declare -p); noninteractive -> disable history is the only issue. And you don't need the remote shell to run yet another, just ssh u@h 'HISTFILE=~/.bash_history; set -o history; history' -- or even quote only the semicolons: ssh u@h HISTFILE=~/.bash_history';'set -o history';'history but that looks really odd
            – dave_thompson_085
            Sep 11 at 3:17











          • Is there a way to make all variables enabled or find out what's disabled when in non-interactive shell?
            – onlineoffline
            Sep 11 at 19:10
















          • what would a working command look like? ssh user@remote.ip "bash history" > testhistory.txt doesn't work
            – onlineoffline
            Sep 10 at 23:24






          • 1




            ssh with additional non-option arguments passes them to the remote shell (all as one -c) so builtins DO work (try ssh u@h declare -p); noninteractive -> disable history is the only issue. And you don't need the remote shell to run yet another, just ssh u@h 'HISTFILE=~/.bash_history; set -o history; history' -- or even quote only the semicolons: ssh u@h HISTFILE=~/.bash_history';'set -o history';'history but that looks really odd
            – dave_thompson_085
            Sep 11 at 3:17











          • Is there a way to make all variables enabled or find out what's disabled when in non-interactive shell?
            – onlineoffline
            Sep 11 at 19:10















          what would a working command look like? ssh user@remote.ip "bash history" > testhistory.txt doesn't work
          – onlineoffline
          Sep 10 at 23:24




          what would a working command look like? ssh user@remote.ip "bash history" > testhistory.txt doesn't work
          – onlineoffline
          Sep 10 at 23:24




          1




          1




          ssh with additional non-option arguments passes them to the remote shell (all as one -c) so builtins DO work (try ssh u@h declare -p); noninteractive -> disable history is the only issue. And you don't need the remote shell to run yet another, just ssh u@h 'HISTFILE=~/.bash_history; set -o history; history' -- or even quote only the semicolons: ssh u@h HISTFILE=~/.bash_history';'set -o history';'history but that looks really odd
          – dave_thompson_085
          Sep 11 at 3:17





          ssh with additional non-option arguments passes them to the remote shell (all as one -c) so builtins DO work (try ssh u@h declare -p); noninteractive -> disable history is the only issue. And you don't need the remote shell to run yet another, just ssh u@h 'HISTFILE=~/.bash_history; set -o history; history' -- or even quote only the semicolons: ssh u@h HISTFILE=~/.bash_history';'set -o history';'history but that looks really odd
          – dave_thompson_085
          Sep 11 at 3:17













          Is there a way to make all variables enabled or find out what's disabled when in non-interactive shell?
          – onlineoffline
          Sep 11 at 19:10




          Is there a way to make all variables enabled or find out what's disabled when in non-interactive shell?
          – onlineoffline
          Sep 11 at 19:10

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468130%2fsome-commands-not-working-when-executed-via-ssh-while-redirecting-output-locally%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