LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2015, 04:25 PM   #1
lump_of_lard
LQ Newbie
 
Registered: Jan 2015
Posts: 1

Rep: Reputation: Disabled
Unhappy Why does BASH history command have no history when run in script ?


So I have this simple BASH script:

# cat test.sh
#!/bin/sh

history -a
history > history_test.out

ls -l history_test.out
ls -l ~/.bash_history



And when I execute the script this is the output I get:

# ./test.sh
-rw-r--r-- 1 root root 0 Jan 29 14:06 history_test.out
-rw------- 1 root root 15028 Jan 29 14:06 /root/.bash_history


So you can see that although root has plenty of history, when run in a script the history command returns empty.

So two questions:

1) Why is history empty when run from script ? Is it because running a script is like running from a new BASH session ?

2) Is there a way to return the root's history from within a script ?

thanks
 
Old 01-29-2015, 05:53 PM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2019 CentOS6.10 CentOS7.6 + 50+ other Linux OS, for test only.
Posts: 17,217

Rep: Reputation: 3552Reputation: 3552Reputation: 3552Reputation: 3552Reputation: 3552Reputation: 3552Reputation: 3552Reputation: 3552Reputation: 3552Reputation: 3552Reputation: 3552
http://unix.stackexchange.com/questi...de-bash-script
The history builtin seems to be disabled inside a shell script.
→ → http://www.tldp.org/LDP/abs/html/histcommands.html
→ Unfortunately, the Bash history tools find no use in scripting.


Found in a second with Google, bash history in script
https://www.google.dk/webhp?hl=all&g...tory+in+script


-

Last edited by knudfl; 01-29-2015 at 05:55 PM.
 
Old 01-30-2015, 03:31 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Because it is designed so.

The bash history is usually stored in home directory of user in .bash_history file.
 
Old 01-30-2015, 05:28 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,810

Rep: Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022Reputation: 4022
yes, it is designed to store only the commands typed in, but nothing else. It will never store anything executed in non-interactive mode.
 
Old 01-30-2015, 09:45 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175Reputation: 3175
The command-line shell interpreter may write a history. But it does so only under certain circumstances. Part of this has to do with the purpose of keeping history ... and with the reality that you can have a large number of bash-processes running in your session at once. If everyone was trying to "write to history," the file would become garbage ... quatch.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash command history, page-up exodist Linux - General 5 04-17-2008 09:28 PM
KDE : Run Command -history denzo74 Linux - General 1 10-19-2005 12:38 AM
F8 for command history in bash titanandrews Linux - General 7 10-15-2004 03:25 PM
Wiping bash command history? skewer Linux - General 3 07-30-2003 02:33 PM
bash command history problem crabboy Slackware 1 10-17-2002 07:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration