4

If I create a file containing

 #!/bin/bash 
 history

and run it, I get no output. Running it manually in a terminal or sourceing the script file does produce output.

Why does history not work from a file?

  • For better understanding I changed the question.. – Sepahrad Salour May 2 '14 at 13:14
14

The second command does "work", but the history is not enabled for non-interactive shells which is why it returns nothing in your script.

Testing the default in a non-interactive shell:

nohistory.sh:

#!/bin/bash
set -o | grep history
history

Result:

$ ./nohistory.sh
history         off

Enabling history with set -o history:

history.sh:

#!/bin/bash
set -o history
set -o | grep history
history

Result:

$ ./history.sh
history         on
    1  set -o | grep history
    2  history

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.