12-12-2018, 05:01 PM
Aliases: DIY Shell Commands
![[Image: aliases-diy-shell-commands.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/12/aliases-diy-shell-commands.jpg)
![[Image: aliases-diy-shell-commands.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/12/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, then the name of the command you want to create, an equal sign (), and then the Bash command(s) you want your alias to run. For example, 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:
where 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, stands for “list (with) color”. Notice there is no space in front of or behind the . Finally, you have the regular Bash command(s) you want to run when is executed. In this case, the command with the option.
After defining your alias, every time you type, the contents of the current directory will be shown in color.
But, you may think, “my 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 instruction without any options to see what aliases you already have. These will vary by distro, but some typical preset aliases are:
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 modifier. For example:
will execute without the option, and
will execute without the 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
to list files or directories.
Likewise,
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
and
Although we haven’t formally introduced 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 example is that it is not a single instruction, but two. The first one () prints out a message reminding you that you misspelled the grep command, then there is a semicolon () that separates one instruction from the other. Finally, you’ve got the second command () that does the actual grepping.
Using on my system to search for the lines containing the word “alias” in /etc/skel/.bashrc, the output looks like this:
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 command:
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!
![[Image: aliases-diy-shell-commands.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/12/aliases-diy-shell-commands.jpg)
![[Image: aliases-diy-shell-commands.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/12/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:
aliasCode:
=Code:
lsCode:
alias lc='ls --color=auto'where
Code:
lcCode:
lcCode:
=Code:
lcCode:
lsCode:
--colorAfter defining your alias, every time you type
Code:
lcBut, you may think, “my
Code:
lsAliases you (probably) already have
Indeed, you can use the
Code:
alias- : You already saw this one above. TheCode:
alias ls='ls --color=auto'modifier of theCode:autooption tellsCode:--colorto use color when standard output is connected to a terminal. That is, the output ofCode:lsis going to show up in a terminal window or a text screen, instead of, say, being piped to a file. Other alternatives forCode:lsareCode:--colorandCode:always.Code:never
- : TheCode:
alias cp='cp -i'option stands for interactive. Sometimes, when you useCode:-iyou may inadvertently overwrite an existing file. By using theCode:cp,Code:-iwill ask you before clobbering anything.Code:cp
- : UsingCode:
alias free='free -m'withCode:-myou 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 ofCode:freeeasier to read for a human.Code:free
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:
\Code:
\freewill execute
Code:
freeCode:
-mCode:
\lswill execute
Code:
lsCode:
--color=autoIf 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:
grepAnother thing to note in the
Code:
gerpCode:
echo "You did it *again*!"Code:
;Code:
grepUsing
Code:
gerpCode:
$ 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_aliasesRunning 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:
unaliasCode:
unalias gerpIf 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!

