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

user310115

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

ssh [email protected] ls > testhistory.txt  
ssh [email protected] "cat .bash_history" > testhistory.txt  

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

ssh [email protected] 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?

msb

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 [email protected] 'echo -e "HISTFILE=~/.bash_history\nset -o history\nhistory" | 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why some commands don't load user environment when executed with ssh? (while other do)

From Dev

Redirecting output of running process via SSH in background

From Dev

nano via SSH commands not working

From Dev

redirecting output of shell to a file with ampersand returns some additional commands in output

From Dev

Streaming output of a remotely executed program via ssh

From Dev

How can I have a graphical 'diff' for git when working locally and also use diff via SSH?

From Dev

output log file from some time to some time via ssh

From Dev

'su user' behavior differs when logged in locally vs via ssh

From Dev

Different output when redirecting

From Dev

ssh-keyscan returns empty output, when executed several times

From Dev

Redirecting output to a file via bash

From Dev

Error 404 when image is uploaded to blob store while working locally

From Dev

Grunt works locally, but not via SSH

From Dev

Some commands are not working in vim

From Dev

Why don't some "for" commands work when the output is piped?

From Dev

Why are SSH commands not working?

From Dev

What commands does git use when communicating via ssh?

From Dev

cant use commands `date` or `find` when executing a script via ssh

From Dev

Proper Quoting of Strings When Executing Remote Commands via SSH

From Dev

PHP Mail not working when redirecting

From Dev

Python subprocess.check_output(args) fails, while args executed via Windows command line work OK

From Dev

can a locally executed script run commands on the remote server?

From Dev

Initial output of parent is lost while redirecting

From Dev

console commands not executed while dubug the tizen app

From Dev

How to get output from upstart jobs when logged in via SSH?

From Dev

How to get output from upstart jobs when logged in via SSH?

From Dev

Powershell is cutting off long output when connected to a terminal via SSH

From Dev

Redirecting output in rc.local not working

From Dev

Unable to SSH over WiFi, working when connected via LAN

Related Related

  1. 1

    Why some commands don't load user environment when executed with ssh? (while other do)

  2. 2

    Redirecting output of running process via SSH in background

  3. 3

    nano via SSH commands not working

  4. 4

    redirecting output of shell to a file with ampersand returns some additional commands in output

  5. 5

    Streaming output of a remotely executed program via ssh

  6. 6

    How can I have a graphical 'diff' for git when working locally and also use diff via SSH?

  7. 7

    output log file from some time to some time via ssh

  8. 8

    'su user' behavior differs when logged in locally vs via ssh

  9. 9

    Different output when redirecting

  10. 10

    ssh-keyscan returns empty output, when executed several times

  11. 11

    Redirecting output to a file via bash

  12. 12

    Error 404 when image is uploaded to blob store while working locally

  13. 13

    Grunt works locally, but not via SSH

  14. 14

    Some commands are not working in vim

  15. 15

    Why don't some "for" commands work when the output is piped?

  16. 16

    Why are SSH commands not working?

  17. 17

    What commands does git use when communicating via ssh?

  18. 18

    cant use commands `date` or `find` when executing a script via ssh

  19. 19

    Proper Quoting of Strings When Executing Remote Commands via SSH

  20. 20

    PHP Mail not working when redirecting

  21. 21

    Python subprocess.check_output(args) fails, while args executed via Windows command line work OK

  22. 22

    can a locally executed script run commands on the remote server?

  23. 23

    Initial output of parent is lost while redirecting

  24. 24

    console commands not executed while dubug the tizen app

  25. 25

    How to get output from upstart jobs when logged in via SSH?

  26. 26

    How to get output from upstart jobs when logged in via SSH?

  27. 27

    Powershell is cutting off long output when connected to a terminal via SSH

  28. 28

    Redirecting output in rc.local not working

  29. 29

    Unable to SSH over WiFi, working when connected via LAN

HotTag

Archive