How to quickly add aliases to your .bashrc file?
Every Linux user will at some point realize what a powerful tool the shell is. Almost anything can be done via a shell like Bash, which comes as default in most Linux distributions.
Aliases and functions are shortcuts and mini-programs that you can add in your .bashrc file, which sits in your home directory. The file can of course be edited with any text editor, and of course that is a good idea if you want to do some structuring or write longer functions. However, sometimes it is nice to just quickly dump a command there as an alias, with one command from the command line.
For example, to get a comprehensive log from SVN with merge information, I typically use
svn log -gvr 1:head
A bit long to type, so instead I have an alias, 'svnlog', which can be quickly created like this:
echo 'alias svnlog="svn log -gvr 1:head"' >> ~/.bashrc
This method will append the alias to the end of your .bashrc file. I like to call this section "the dump" because it has just general mismatch of random commands, which occasionally I move to their correct sections for clarity. Think of it as an alias inbox. Basically any command you want to save as an alias, just wrap it like this, replacing name for alias name and command for the command (remember to escape any quotation marks in the command):
echo 'alias name="command"' >> ~/.bashrc
If you want to have to command available instantly, just add '&& . ~/.bashrc' to the end to reload your .bashrc.
echo 'alias name="command"' >> ~/.bashrc && . ~/.bashrc
Another useful example for any user is an alias which chains apt-get update and upgrade to run after each other, useful for quick system updates.
echo 'alias sup="sudo apt-get update; sudo apt-get upgrade"' >> ~/.bashrc && . ~/.bashrc
OK, it's a long command, how do I remember it?
Add this to your .bashrc file and then just type ald it to get a reminder.
alias ald="echo \"echo 'alias name=\"command\"' >> ~/.bashrc && . ~/.bashrc\""












Comments (post a comment)