Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is sort of a contiunation of my last question: printing saving the last bash input command

Now I want to know if it's possible to know when you're at the first bash prompt of a terminal. So I'm displaying the last command run above the current prompt. And I'm thinking it would be cool if when you're at a fresh terminal, to display the last N commands.

I guess I could just echo the last commands inside my .bashrc, but that doesn't seem like a good idea. And also it'd be cool to get the last N commands shown when the scrollback is cleared (via clear).

Is any of that (reasonably) possible?

share|improve this question
1  
I don't understand your question. Does “the first bash prompt of a terminal” mean the first prompt after you start bash? Or do you mean if bash is running directly under the terminal, as opposed to being started by some other program (like another instance of bash)? Are you just trying to write a function to invoke manually, is part of your question invoking it upon some trigger (which)? Have you experimented a little with the fc and history built-ins? Are you prepared to switch to zsh if you're asking too much of bash? –  Gilles Oct 16 '10 at 19:58
add comment

2 Answers

up vote 1 down vote accepted

I guess I could just echo the last commands inside my .bashrc, but that doesn't seem like a good idea. And also it'd be cool to get the last N commands shown when the scrollback is cleared (via clear).

I'm not sure there's another way to do it without modifying the bash source... What seems bad about putting it in .bashrc though? You should just add an interactivity test so it doesn't print it in any script :

if [ ! -z "$PS1" ]; then
    echo "Hello, the last 5 commands you typed were :"
    tail -n5 ~/.bash_history
fi

You could also create an alias for clear which basically does the same.

share|improve this answer
    
I like the idea of adding a clear alias –  Falmarri Oct 19 '10 at 17:56
    
How about history 5, in case the history file has been configured to something other than ~/.bash_history? Less to type, too. –  ephemient Oct 21 '10 at 13:32
add comment

http://www.thegeekstuff.com/2008/09/bash-shell-ps1-10-examples-to-make-your-linux-prompt-like-angelina-jolie/

Scroll all the way to the bottom of the page. It talks about a \# code that displays the "command number." It starts at 1 and increments for each command you enter until you exit the terminal. I've found it useful.

share|improve this answer
add comment

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.