InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Redefining Commands with alias in bash
Bash

Redefining Commands with alias in bash

InfinityCoder February 20, 2017

You’d like to slightly alter the definition of a command, perhaps so that you always use a particular option on a command (e.g., always using -a on the ls command or -i on the rm command).

Use the alias feature of bash for interactive shells (only).

The alias command is smart enough not to go into an endless loop when you say something like:

1
alias ls='ls -a'

In fact, just type alias with no other arguments and you can see a list of aliases that are already defined for you in your bash session.

Some installations may already have several available for you.

The alias mechanism is a straightforward text substitution. It occurs very early in the command-line processing, so other substitutions will occur after the alias.

For example, if you want to define the single letter “h” to be the command that lists your home directory, you can do it like this:

1
alias h='ls $HOME'

or like this:

1
alias h='ls ~'

The use of single quotes is significant in the first instance, meaning that the variable $HOME will not be evaluated when the definition of the alias is made.

Only when you run the command will the (string) substitution be made, and only then will the $HOME variable be evaluated.

That way if you change the definition of $HOME the alias will move with it, so to speak.
If, instead, you used double quotes, then the substitution of the variable’s value would be made right away and the alias would be defined with the value of $HOME substituted.

You can see this by typing alias with no arguments so that bash lists all the alias definitions.

You would see something like this:

1
2
3
...
alias h='ls /home/youracct'
...

If you don’t like what your alias does and want to get rid of it, just use unalias and the name of the alias that you no longer want.

For example:

1
unalias h

will remove the definition that we just made above. If you get really messed up, you can use unalias -a to remove all the alias definitions in your current shell session.
But what if someone has created an alias for unalias? Simple, if you prefix it with a backslash, alias expansion is not performed.

So use \unalias -a instead.
Aliases do not allow arguments.

For example, you cannot do this:

1
2
# Does NOT work, arguments NOT allowed
$ alias='mkdir $1 && cd $1'

The difference between $1 and $HOME is that $HOME is defined (one way or another) when the alias itself is defined, while you’d expect $1 to be passed in at runtime.
Sorry, that doesn’t work. Use a function instead.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Setting Shell History Options in bash
You’d like more control over your command-line history. Set the …

Setting Shell History Options in bash

Keeping Some Output, Discarding the Rest in bash
You need a way to keep some of your output …

Keeping Some Output, Discarding the Rest in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap