Create an account


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

#1
Aliases: DIY Shell Commands

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/12/aliases-diy-shell-commands.jpg" width="1500" height="858" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/12/aliases-diy-shell-commands.jpg" class="ff-og-image-inserted" /></div>
<p>Aliases, in the context of the Linux shell, are <b>commands you build yourself</b> by packing them with combinations of other instructions that are too long or too hard to remember.</p>
<p>You create an alias by using the word <code>alias</code>, then the name of the command you want to create, an equal sign (<code>=</code>), and then the Bash command(s) you want your alias to run. For example, <code>ls</code> 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:</p>
<pre>
alias lc='ls --color=auto'
</pre>
<p>where <code>lc</code> 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</code> stands for “list (with) color”. Notice there is no space in front of or behind the <code>=</code>. Finally, you have the regular Bash command(s) you want to run when <code>lc</code> is executed. In this case, the <code>ls</code> command with the <code>--color</code> option.</p>
<p>After defining your alias, every time you type <code>lc</code>, the contents of the current directory will be shown in color.</p>
<p>But, you may think, “my <code>ls</code> command already lists files in different colors!” That is because most Linux distros come with some aliases already set up for you.</p>
<h3>Aliases you (probably) already have</h3>
<p>Indeed, you can use the <code>alias</code> instruction without any options to see what aliases you already have. These will vary by distro, but some typical preset aliases are:</p>
<ul>
<li><code>alias ls='ls --color=auto'</code>: You already saw this one above. The <code>auto</code> modifier of the <code>--color</code> option tells <code>ls</code> to use color when standard output is connected to a terminal. That is, the output of <code>ls</code> 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</code> are <code>always</code> and <code>never</code>.</li>
<li><code>alias cp='cp -i'</code>: The <code>-i</code> option stands for <i>interactive</i>. Sometimes, when you use <code>cp</code> you may inadvertently overwrite an existing file. By using the <code>-i</code>, <code>cp</code> will ask you before clobbering anything.</li>
<li><code>alias free='free -m'</code>: Using <code>-m</code> with <code>free</code>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</code> easier to read for a human.</li>
</ul>
<p>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> modifier. For example:</p>
<pre>
\free
</pre>
<p>will execute <code>free</code> without the <code>-m</code> option, and</p>
<pre>
\ls
</pre>
<p>will execute <code>ls</code> without the <code>--color=auto</code> option.</p>
<p>If you want to get rid or modify the preset aliases forever, note that they live in the global <i>.bashrc</i> file which hangs out in <a href="https://www.linux.com/learn/intro-to-linux/2018/7/users-groups-and-other-linux-beasts">our old haunt, the <i>/etc/skel</i> directory</a>.</p>
<h2>Aliases for muscle memory</h2>
<p>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</p>
<pre>
alias dir='ls'
</pre>
<p>to list files or directories.</p>
<p>Likewise,</p>
<pre>
alias copy='cp'
alias move='mv'
</pre>
<p>may also come in handy, at least until you get used to Linux’s new lexicon.</p>
<p>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 <i>admnis-</i>… <i>adminsi-</i>… <i>A-D-M-I-N-I-S-T-R-A-T-I-ON</i> (<i>phew!</i>) at speed. That is why some users create aliases like</p>
<pre>
alias sl='ls'
</pre>
<p>and</p>
<pre>
alias gerp='echo "You did it *again*!"; grep'
</pre>
<p>Although we haven’t formally introduced <code>grep</code> 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.</p>
<p>Another thing to note in the <code>gerp</code> example is that it is not a single instruction, but two. The first one (<code>echo "You did it *again*!"</code>) prints out a message reminding you that you misspelled the grep command, then there is a semicolon (<code>;</code>) that separates one instruction from the other. Finally, you’ve got the second command (<code>grep</code>) that does the actual grepping.</p>
<p>Using <code>gerp</code> on my system to search for the lines containing the word “<i>alias</i>” in <i>/etc/skel/.bashrc</i>, the output looks like this:</p>
<pre>
$ <b>gerp -R alias /etc/skel/.bashrc</b>
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
</pre>
<p>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.</p>
<p>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</code> command:</p>
<pre>
unalias gerp
</pre>
<p>If you want to make your aliases permanent, you can drop them into the <i>.bashrc</i> file you have in your home directory. This is the same thing we did with custom environment variables in <a href="https://www.linux.com/blog/learn/2018/12/bash-variables-environmental-and-otherwise">last week’s article</a>.</p>
<p>See you next time!</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016