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 might be a bad idea. The more I think about it the more I come to the realization that I probably shouldn't do it... but I've been trying and failing so I REALLY want to know how to do it, even if it's a bad idea.

What I want is for the bashrc file to be sourced every time I run the clear command. The reason for this is completely materialistic. I have system information echoed out when I source bashrc and it's cool to me and I'd like that to be at the top every time I clear.
I've tried to set up some aliases for clear but I keep running into infinite loops. The obvious fix is to change the aliases to something else besides clear so that I can run the clear command in the alias without interfering but I type clear so often that it's kind of ingrained in my brain at this point. I'd like to be able to type clear and make it clear AND source the bashrc file.

share|improve this question
2  
Make sure your .bashrc script has no cumulative effects. If it does anything like appending things to your paths, it could get messy. In general, .bashrc is for initialising your shell environment. if it does other things, then it would be better to split them out into a new script and call that along with clear. Also, if anyone else uses your system or assists you, they may encounter unexpected behaviour because you have repurposed a common command. –  Joe Feb 4 '12 at 0:24
    
@Joe That's a good point about other people using my system. You guys have brought up alot of things I hadn't thought about. –  CaldwellYSR Feb 4 '12 at 15:38
add comment

4 Answers 4

up vote 24 down vote accepted

alias clear='source ~/.bashrc; \clear'

The \ tells bash that you want to invoke the external command, not the alias.

share|improve this answer
    
Ah great, I knew it would be something simple like that! –  CaldwellYSR Feb 3 '12 at 6:16
12  
You probably want to do this in the other order, or the clear will erase any output that .bashrc generated. But it's also a bad idea to have .bashrc generate output at all. –  cjm Feb 3 '12 at 7:29
    
@cjm Yeah, I realized that this morning about the order. I already had it in the correct order, I just didn't have the ` in front of clear` I'll look at that information about the bashrc and output. Thanks for the link. –  CaldwellYSR Feb 3 '12 at 14:29
    
an alternative to \clear is command clear –  glenn jackman Feb 3 '12 at 14:58
    
@CaldwellYSR, the short version is that ssh host command sources .bashrc. If that generates output, it'll confuse any scripts trying to run remote commands on your host & account. You might not be doing that now, but someday you might, and then you'll be confused why it doesn't work. –  cjm Feb 3 '12 at 16:29
add comment

Write a script that prints the system information you want. Invoke it from .bashrc and from your clear alias.

share|improve this answer
add comment

In your .bashrc set an alias for clear that sources .bashrc afterward:

alias clear='clear && . ~/.bashrc'
share|improve this answer
add comment

Per Joe's comment... if you have "cumulative effects" (your profile isn't safe to rerun multiple times), here's a hack to get around that!

alias clear='\clear; exec bash -l'

This really starts a new interpreter every time you clear, guaranteeing it's really a fresh start in every way. The exec though, means you don't with tons of processes lying around later.

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.