Aliases - HowTo

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Habitual

Aliases - HowTo

Post by Habitual »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 4 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
SimonTS

Re: Aliases - HowTo

Post by SimonTS »

@ 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:
Habitual

Re: Aliases - HowTo

Post by Habitual »

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.
mockturtl

Re: Aliases - HowTo

Post by mockturtl »

Thanks!
Habitual

Re: Aliases - HowTo

Post by Habitual »

You're welcome.

Tomorrow, we run with scissors, oh boy! :wink:
SimonTS

Re: Aliases - HowTo

Post by SimonTS »

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

Re: Aliases - HowTo

Post by spider2097 »

@ 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:
SimonTS

Re: Aliases - HowTo

Post by SimonTS »

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

Re: Aliases - HowTo

Post by spider2097 »

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
SimonTS

Re: Aliases - HowTo

Post by SimonTS »

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.
Habitual

Re: Aliases - HowTo

Post by Habitual »

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
SimonTS

Re: Aliases - HowTo

Post by SimonTS »

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.
Habitual

Re: Aliases - HowTo

Post by Habitual »

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!
spider2097

Re: Aliases - HowTo

Post by spider2097 »

Thanks :)

I guess you can't pipe an alias then?
SimonTS

Re: Aliases - HowTo

Post by SimonTS »

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

Re: Aliases - HowTo

Post by JerryLuke »

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
Habitual

Re: Aliases - HowTo

Post by Habitual »

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".
Locked

Return to “Scripts & Bash”