Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
For better understanding I changed the question.. – Sepahrad Salour May 2 '14 at 13:14
up vote 11 down vote accepted

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

$ cat nohistory.sh
#!/bin/bash
set -o | grep history
history

.

$ ./nohistory.sh
history         off

.

$ cat history.sh
#!/bin/bash
set -o history
set -o | grep history
history

.

$ ./history.sh
history         on
    1  set -o | grep history
    2  history
share|improve this answer
1  
Thank your for your great answer :) – Sepahrad Salour May 2 '14 at 13:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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