Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Aliases: DIY Shell Commands
1
Aliases: DIY Shell Commands

[Image: aliases-diy-shell-commands.jpg]

[Image: aliases-diy-shell-commands.jpg]

Aliases, in the context of the Linux shell, are commands you build yourself by packing them with combinations of other instructions that are too long or too hard to remember.

You create an alias by using the word
Code:
alias
, then the name of the command you want to create, an equal sign (
Code:
=
), and then the Bash command(s) you want your alias to run. For example,
Code:
ls
in its base form does not colorize its output, making it difficult to distinguish between directories, files, and links. You can build a new command that shows colors by making an alias like this:

Code:
alias lc='ls --color=auto'

where 
Code:
lc
is the name you have picked for your new command. When creating aliases, be sure to check that the name you picked isn’t already in use, or you may override an existing command. In this case,
Code:
lc
stands for “list (with) color”. Notice there is no space in front of or behind the
Code:
=
. Finally, you have the regular Bash command(s) you want to run when
Code:
lc
is executed. In this case, the
Code:
ls
command with the
Code:
--color
option.

After defining your alias, every time you type
Code:
lc
, the contents of the current directory will be shown in color.

But, you may think, “my
Code:
ls
command already lists files in different colors!” That is because most Linux distros come with some aliases already set up for you.

Aliases you (probably) already have

Indeed, you can use the
Code:
alias
instruction without any options to see what aliases you already have. These will vary by distro, but some typical preset aliases are:
  • Code:
    alias ls='ls --color=auto'
    : You already saw this one above. The
    Code:
    auto
    modifier of the
    Code:
    --color
    option tells
    Code:
    ls
    to use color when standard output is connected to a terminal. That is, the output of
    Code:
    ls
    is going to show up in a terminal window or a text screen, instead of, say, being piped to a file. Other alternatives for
    Code:
    --color
    are
    Code:
    always
    and
    Code:
    never
    .

  • Code:
    alias cp='cp -i'
    : The
    Code:
    -i
    option stands for interactive. Sometimes, when you use
    Code:
    cp
    you may inadvertently overwrite an existing file. By using the
    Code:
    -i
    ,
    Code:
    cp
    will ask you before clobbering anything.

  • Code:
    alias free='free -m'
    : Using
    Code:
    -m
    with
    Code:
    free
    you can see how much free memory you have and how much your applications are using in megabytes instead of the default bytes. This makes the output of
    Code:
    free
    easier to read for a human.

There may be more (or less, or even none), but regardless of what your distribution comes with, you can always use the base form (vs. the aliased form) of a command with the
Code:
\
modifier. For example:

Code:
\free

will execute
Code:
free
without the
Code:
-m
option, and

Code:
\ls

will execute
Code:
ls
without the
Code:
--color=auto
option.

If you want to get rid or modify the preset aliases forever, note that they live in the global .bashrc file which hangs out in our old haunt, the /etc/skel directory.

Aliases for muscle memory

Distro designers try their best to predict which aliases are going to be useful for you. But every user is different and comes from a different background. If you are new to GNU+Linux, it may be because you are coming from another system, and the basic commands vary from shell to shell. If you come from a Windows/MS-DOS background, you may want to define an alias like

Code:
alias dir='ls'

to list files or directories.

Likewise,

Code:
alias copy='cp' alias move='mv'

may also come in handy, at least until you get used to Linux’s new lexicon.

The other problem occurs when mistakes become ingrained in your muscle memory, so you always mistype some words the same way. I, for instance, have great difficulty typing admnis-… adminsi-… A-D-M-I-N-I-S-T-R-A-T-I-ON (phew!) at speed. That is why some users create aliases like

Code:
alias sl='ls'

and

Code:
alias gerp='echo "You did it *again*!"; grep'

Although we haven’t formally introduced
Code:
grep
yet, in its most basic form, it looks for a string of characters in a file or a set of files. It’s one of those commands that you will tend to use A LOT once you get to grips with it, as those ingrained mistyping habits that force you to type the instruction twice every time get annoying really quickly.

Another thing to note in the
Code:
gerp
example is that it is not a single instruction, but two. The first one (
Code:
echo "You did it *again*!"
) prints out a message reminding you that you misspelled the grep command, then there is a semicolon (
Code:
;
) that separates one instruction from the other. Finally, you’ve got the second command (
Code:
grep
) that does the actual grepping.

Using
Code:
gerp
on my system to search for the lines containing the word “alias” in /etc/skel/.bashrc, the output looks like this:

Code:
$ gerp -R alias /etc/skel/.bashrc You did it *again*! alias ls='ls --color=auto' alias grep='grep --colour=auto' alias egrep='egrep --colour=auto' alias fgrep='fgrep --colour=auto' alias cp="cp -i" alias df='df -h' alias free='free -m' alias np='nano -w PKGBUILD' alias more=less shopt -s expand_aliases

Running commands sequentially as part of an alias, or, even better, chaining commands so that one command can use the results coughed up by another, is getting us perilously close to Bash scripting. This has been in the making of this series for quite some time, and we’ll start covering it in the very next article.

For the time being, if you want to get rid of an alias you temporarily set up in a running terminal, use the
Code:
unalias
command:

Code:
unalias gerp

If you want to make your aliases permanent, you can drop them into the .bashrc file you have in your home directory. This is the same thing we did with custom environment variables in last week’s article.

See you next time!
Reply



Forum Jump:


Users browsing this thread: 1 Guest(s)