Aliases - HowTo

Forum rules
Before you post please read how to get help
User avatar
Habitual
Level 12
Level 12
Posts: 4077
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Aliases - HowTo

Postby Habitual » Thu Jul 14, 2011 11:14 pm

Aliases are like speed dialing.
Think of any command that is long that you have to type out regularly (and it bakes your banana to remember the entire command like it does me)

Wouldn't you rather type "sahara" than "ssh -qi /home/jj/.ssh/sahara/keypair/id_rsa-keypair root@xxx.xxx.xxx.xxx" ? I'm assuming you would.

Let's do some math on what may seem a trivial exercise, and you'll see what I mean.

Take the command "exit" (from your favorite terminal).
4 characters + 1 for the enter key = 5 keystrokes. 5 keys you have to hit every time you exit the terminal. No big deal? Let's see...
What if you have to type "exit" (never mind you type 400 WPM at 98% accuracy) 100 times a day. That's 500 keystrokes to exit 100 terminal windows every day. 500 keystrokes for 100 terminals-a-day for 365 days a year is 182,500 keystrokes a year. That's a LOT of repetitive keystrokes.

Using an alias you could cut that down to 500, for the whole year. Amazing. How? alias x=`exit` in your .bashrc
Using the "x" alias instead of the "exit" command, you are a couple of hundred times more proficient! Employers LOVE proficiency. :wink:

I have several alias files defined in my .bashrc
One for all my work ssh commands and another for my aliases that I use every day.

Here's a few that I used when I had LinuxMint installed and you should benefit from also. This will get this thread started. :)
The #s are comments and are NOT processed by your .bashrc or other alias file.
To use these in your .bashrc just open a terminal and type

Code: Select all

vi + .bashrc
Then the letter o (new line and insert mode) and paste these into the bottom of your .bashrc

Code: Select all

### apt-related
# alias install="sudo apt-get install -y"
# alias search="sudo apt-cache search"
# alias remove="sudo apt-get remove"
# alias clean="sudo apt-get autoremove"
# alias update="sudo apt-get update"
# alias upgrade="sudo apt-get upgrade"
## end apt-related
alias reload='source ~/.bashrc'


Press ESC once in vi and then ZZ to save and exit vi
Back at the prompt type

Code: Select all

source ~/.bashrc
and if you edited correctly, you should not see any errors and be in business.
Next time you have to edit your .bashrc to add another alias, just ESC > ZZ from vi and type

Code: Select all

reload


Enjoy!

Edit: Mon Jul 18, 2011
Tips and Tricks I have learned:

Use double quotes around complex aliases
If the alias contains a double-quoted command or string, use a \ to escape the first 'inner' double-quote and the last 'outer' double-quote.
Example:

Code: Select all

alias workssh="alias  | grep ssh | cut -d= -f1 | egrep -v \"workssh|go"\"

To negate an alias in an alias, you can prepend it with a \
My actual alias for workssh is

Code: Select all

alias workssh="alias  | \grep ssh | cut -d= -f1 | \egrep -v \"workssh|go"\"

where I escape my own grep alias with \grep (my grep alias includes colorized output)

To list the aliases construction type

Code: Select all

alias <alias_name>

To list all aliases, well just type

Code: Select all

alias
at the c-li

To unset an alias (they can get unwieldy) type

Code: Select all

unset <alias_name>


To be continued...
Last edited by Habitual on Mon Jul 18, 2011 12:37 pm, edited 3 times in total.
If you want the things you've never had,
You have to do things you've never done.

User avatar
SimonTS
Level 6
Level 6
Posts: 1271
Joined: Thu Feb 24, 2011 5:19 pm
Location: Bristol, United Kingdom

Re: Aliases - HowTo

Postby SimonTS » Fri Jul 15, 2011 4:53 pm

@ Habitual;-

Thanks for this - you just stopped me from being so damn lazy. I have been saying to myself for months that I needed to look up where to put my aliases to make them permanent, but something else always came up and got in the way. My most common one is 'alias l="ls -al"' and I end up typing this in most days coz I was being too lazy :shock:

User avatar
Habitual
Level 12
Level 12
Posts: 4077
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Re: Aliases - HowTo

Postby Habitual » Fri Jul 15, 2011 8:18 pm

Well, I'm tickled pink that someone got something out of it. :)

To create a separate alias file for your own "amusement"...

Code: Select all

vi ~/.bashrc

and add
source /home/jj/.aliases
substitute jj for your own userid, of course - NOTE: absolute paths work best, no ~ "cheating"
save and exit.

now

Code: Select all

vi ~/.aliases


add whatever grabs your fancy to it or all of the apt-get ones I posted above...
save and exit.

source ./bashrc to reload it.

have fun.
If you want the things you've never had,
You have to do things you've never done.

User avatar
mockturtl
Level 4
Level 4
Posts: 453
Joined: Sat Oct 09, 2010 8:51 pm

Re: Aliases - HowTo

Postby mockturtl » Fri Jul 15, 2011 9:00 pm

Thanks!
Image

User avatar
Habitual
Level 12
Level 12
Posts: 4077
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Re: Aliases - HowTo

Postby Habitual » Fri Jul 15, 2011 9:25 pm

You're welcome.

Tomorrow, we run with scissors, oh boy! :wink:
If you want the things you've never had,
You have to do things you've never done.

User avatar
SimonTS
Level 6
Level 6
Posts: 1271
Joined: Thu Feb 24, 2011 5:19 pm
Location: Bristol, United Kingdom

Re: Aliases - HowTo

Postby SimonTS » Sat Jul 16, 2011 6:20 am

I'm not sure that the nurses here will let me play with anything as dangerous as scissors :shock: They still get the heebie-jeebies when I get the crayons out of the box :roll:

spider2097
Level 2
Level 2
Posts: 68
Joined: Thu Dec 03, 2009 11:57 am
Location: Wirral, UK

Re: Aliases - HowTo

Postby spider2097 » Mon Jul 18, 2011 6:13 am

@ Habitual

Thanks for this :) I have to admit I haven't really been a huge user of the CLI or writing my own Bash scripts. I've recently been exploring it more & more as a way of getting things done :) It hadn't occurred to me to set up aliases :shock:

At the risk of being a tad n00bish, I assume you can pipe commands in your aliases?

For instance, recently I've been doing a bit of messing around with installing straight to USB devices and occasionally this leads to grub problems (admittedly this isn't going to be one for everyday use :lol: ) So, instead of having to google "fixing grub" or "reinstalling grub", would it be possible to create an alias such as :

alias fixgrub="sudo update-grub | sudo grub-install /dev/sda"

Is it possible to pass variable arguments through an alias? In this example, could it become :

alias fixgrub(x)="sudo update-grub | sudo grub-install /dev/sd(x)"

(with bracket replaced with square brackets)

Admittedly, it's not exactly the most complicated set of instructions but sometimes it's the more simple ones that elude you! :lol:

User avatar
SimonTS
Level 6
Level 6
Posts: 1271
Joined: Thu Feb 24, 2011 5:19 pm
Location: Bristol, United Kingdom

Re: Aliases - HowTo

Postby SimonTS » Mon Jul 18, 2011 7:02 am

I'm not sure about passing a variable through as I haven't played around with this stuff, but surely the correct method for running the two GRUB commands would actually be;-

Code: Select all

alias fixgrub="sudo grub-install /dev/sda && sudo update-grub"

spider2097
Level 2
Level 2
Posts: 68
Joined: Thu Dec 03, 2009 11:57 am
Location: Wirral, UK

Re: Aliases - HowTo

Postby spider2097 » Mon Jul 18, 2011 8:51 am

SimonTS wrote:I'm not sure about passing a variable through as I haven't played around with this stuff, but surely the correct method for running the two GRUB commands would actually be;-

Code: Select all

alias fixgrub="sudo grub-install /dev/sda && sudo update-grub"

Aaah :) I'm guessing that comes down to the difference in the operands? I thought that by using pipe, the two commands would run consecutively :?: I could always be misunderstanding - it's a long time since I used CLI & script operands at university and it's only recently that I've started using the CLI in the two and half years I've been using Linux for now (after quite a while being used by Windows :lol: ). By that I mean properly using the CLI, not just copying & pasting but trying to understand exactly what it's doing. :D

User avatar
SimonTS
Level 6
Level 6
Posts: 1271
Joined: Thu Feb 24, 2011 5:19 pm
Location: Bristol, United Kingdom

Re: Aliases - HowTo

Postby SimonTS » Mon Jul 18, 2011 9:02 am

I am willing to be corrected if I'm wrong, but I understand a pipe '|' to effectively be a "run the first command through the second", e.g.

Code: Select all

sudo find |grep logrotate

will find all files (starting from the current location) then pipe that output through the second part, thus only the files with 'logrotate' in them will be output to screen.

Edited for spelling mistake caused by typing on Android phone :oops:
Last edited by SimonTS on Mon Jul 18, 2011 10:46 am, edited 1 time in total.

User avatar
Habitual
Level 12
Level 12
Posts: 4077
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Re: Aliases - HowTo

Postby Habitual » Mon Jul 18, 2011 10:40 am

SimonTS wrote:I am willing to be corrected if I'm wrong, but I understand a pie '|' to effectively be a "run the first command through the second", e.g.

Code: Select all

sudo find |grep logrotate

will find all files (starting from the current location) then pipe that output through the second part, thus only the files with 'logrotate' in them will be output to screen.


SimonTS: The | symbol is a pipe, not pie. and yes, the 2nd is piped into the results of the first.
Is yours not working?
Lemme know ...

JJ
If you want the things you've never had,
You have to do things you've never done.

User avatar
SimonTS
Level 6
Level 6
Posts: 1271
Joined: Thu Feb 24, 2011 5:19 pm
Location: Bristol, United Kingdom

Re: Aliases - HowTo

Postby SimonTS » Mon Jul 18, 2011 10:49 am

I've edited my post above due to the spelling mistake. Caused by typing on my Android phone (or maybe by being too hungry when I was doing it).

Mine works fine - I was just trying to guide 'Spider' with my limited knowledge.

User avatar
Habitual
Level 12
Level 12
Posts: 4077
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Re: Aliases - HowTo

Postby Habitual » Mon Jul 18, 2011 11:05 am

SimonTS wrote:I've edited my post above due to the spelling mistake. Caused by typing on my Android phone (or maybe by being too hungry when I was doing it).

Mine works fine - I was just trying to guide 'Spider' with my limited knowledge.

Understood!
If you want the things you've never had,
You have to do things you've never done.

spider2097
Level 2
Level 2
Posts: 68
Joined: Thu Dec 03, 2009 11:57 am
Location: Wirral, UK

Re: Aliases - HowTo

Postby spider2097 » Mon Jul 18, 2011 5:21 pm

Thanks :)

I guess you can't pipe an alias then?

User avatar
SimonTS
Level 6
Level 6
Posts: 1271
Joined: Thu Feb 24, 2011 5:19 pm
Location: Bristol, United Kingdom

Re: Aliases - HowTo

Postby SimonTS » Mon Jul 18, 2011 5:29 pm

Yes, you can use a pipe in an alias, just like you can use any other operand, e.g.

Code: Select all

alias search="find | grep .txt"

works perfectly.

JerryLuke
Level 1
Level 1
Posts: 18
Joined: Tue Feb 22, 2011 4:38 pm

Re: Aliases - HowTo

Postby JerryLuke » Mon Jul 18, 2011 7:45 pm

This is great! I've been wanting to learn more about aliases for some time. I'll be studying these pages in great detail!

Jerry Luke
Oregon, USA

User avatar
Habitual
Level 12
Level 12
Posts: 4077
Joined: Sun Nov 21, 2010 8:31 pm
Location: Youngstown, Ohio

Re: Aliases - HowTo

Postby Habitual » Mon Jul 18, 2011 7:56 pm

Well, if anyone gets stuck, shoot me a PM and I'll help out where I can, or give pointers.
Occasionally, I will question the logic of the task, or your critical thinking. :)

Usually, I don't like to mess around and just throw out the answer/solution tho'.

I HATE "Fluff".
If you want the things you've never had,
You have to do things you've never done.


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 1 guest