Sunday, October 15, 2006

alias Shorten Long Linux Command

Tired of typing Linux command because of long list of command option switches? Try to use the command alias to relieve tired of typing long Linux command then.

As the command name suggests, the Linux shell built-in command alias allows a user-defined command to represent

  1. A system command with long arguments, such as

    find . -type d -print

    This find command used with a long option switches to list all directories that exists from the current directory downwards. The find command could be rather long if more find command option switches applied.

    This long command line could be made simple with an alias, such as

    alias dirfind='find . -type d -print'

    that allows user to execute the short user-defined command dirfind instead of typing out that long command line.

  2. A series of system commands, such as

    tar -zcvpf bin.tgz bin; [ $? -eq 0 ] && echo DONE || echo FAIL

    These system commands execute sequentially, aim to check the tarball archive creation status upon its completion. If success then echo DONE else echo FAIL.

    Again, by using the alias command could make it shorter and simple enough, such as

    alias chkbkp='tar -zcvpf bin.tgz bin; [ $? -eq 0 ] && echo DONE || echo FAIL'

    where the user could execute chkbkp at command prompt and the Linux shell will expanding it to that commands series.
Related information:
  • It is not advisable to declare and use alias in shell scripts. It is better to use a variable or a function for the purpose. Anyway, if insist, it is technically possible to use alias in shell scripts by turning on shell scripts option shopt -s expand_aliases before declaring and using an alias inside the shell scripts, such as the sample in diagram below.

  • Use alias in bash shell scripts with shopt -s expand_aliases enabled

  • By default, the parent shell where a shell scripts executed will always spawn the scripts execution into a subshell, unless explicitly run the shell scripts in the current shell environment. Shell scripts that is running in subshell will not able to use aliases defined in parent shell environment!

    To run a shell scripts in current shell environment instead of spawning into subshell, use one of these two syntax

    source testscripts.sh
                or
    . testscripts.sh

  • How to run shell scripts in subshell. How to run shell scripts in the current shell?

  • In Linux system, there is a file named .bashrc in each home directory. Use this file to define aliases, which will make the defined aliases effective on each login.
  • The login profile .bash_profile could also be used to define aliases. In fact, it is the .bash_profile which execute the .bashrc while a user login.
  • To remove an alias, simply use the shell built-in command unalias, such as unalias chkbkp
  • To list all the aliases defined in the current login session, just type the command alias at the command prompt will do.
  • Which command is the Linux shell refers to
  • Search more related info with Google Search engine built-in

This article has no comments yet. Why don't write your comments for this article?