Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-15-2012, 11:03 AM   #1
LQ Newbie
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
simple history command - help

Hi guessing this is really simple, but i just can get it to work

ive created a bash script which i just want to display the bash history

if i type the word history in the terminal is displays all the history as expected....but if i put this in a bash script and run the bash script it doesnt display the information. i have tried both:-


ubuntu@john-laptop:~$ history



can anyone explain what im doing wrong please? Thank you
Old 04-15-2012, 11:14 AM   #2
LQ 5k Club
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,528

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
cat /home/user/.bash_history

Kind regards
Old 04-15-2012, 11:20 AM   #3
Registered: Dec 2010
Location: iran
Posts: 179

Rep: Reputation: 0
try this

echo $(command)

for example :
echo $(ls)
Old 04-16-2012, 12:50 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Command history is only loaded in interactive sessions. Scripts don't run in interactive shells by default.

If you run the script with "#!/bin/bash -i" at the top, the script's environment will be made interactive, and your (pre-existing) history file will be loaded into it. Then your history command will work as expected. Or just read the file directly, as repo showed above.

This will, however, only give you the history list from the last time the file was saved (usually at the close of the last session). To get your current shell's history you have to run the command directly in that shell.

Perhaps if you explained why you want to display the history, we could help you to a proper solution. Maybe something involving a function in the current shell would do instead, for example?

PS: Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Last edited by David the H.; 04-16-2012 at 12:52 AM.
1 members found this post helpful.


bash, history

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
history command in hp ux abhijeetdutta Other *NIX 8 05-22-2014 02:10 PM
[SOLVED] Is there any command to delete bash command history? cola Linux - Newbie 4 06-01-2010 07:32 AM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 02:14 AM
History command amer_58 Linux - General 5 06-24-2005 12:26 PM
Command history? King4lex Linux - Newbie 4 09-03-2004 08:35 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:09 PM.

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