13

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.

2
  • 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, 2012 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. Feb 4, 2012 at 15:38

4 Answers 4

27

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

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

5
  • Ah great, I knew it would be something simple like that! Feb 3, 2012 at 6:16
  • 13
    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, 2012 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. Feb 3, 2012 at 14:29
  • an alternative to \clear is command clear Feb 3, 2012 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, 2012 at 16:29
2

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

1

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

alias clear='clear && . ~/.bashrc'
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .