Quantcast
Viewing all articles
Browse latest Browse all 6

Shell Tip: Run an aliased command, ignoring the alias (Solution #0)

How Bash Uses Aliases/Commands

First, it will try to find an alias for the given name.  If no alias is found, then it will use the first command in your $PATH by the given name.

Scenario

You set default options for cmd-x by creating an alias with the same name (cmd-x).  In this example, the alias and command are both called “grep”.

$> alias grep="grep --color"

BUT!  What if you want to run grep without the “–color” option?

Solution #0: Escape a letter in the command name with “\”

$> \grep #...

This successfully completes our goal.  The shell does not use the alias, because “\grep” does not match any of our alias names.

It is still able to find grep in our $PATH, because escaping a character tells the shell to use the character literally.  In other words, “\g” is the same thing as “g”. (for proof see: How Bash Treats Escaped Letters)

How Bash Treats Escaped Letters

Here is proof that bash will treat “\grep” and “grep” the same.

   # OPTIONS FOR diff:
   # -q only tell us if the files are different, not all of the differences
   # -s tell us if the files are identical
   #    (by default, diff only tells the differences between files
   #     and silently ignores files that are the same)

$> diff -sq <(echo grep) <(echo \g\r\e\p)
Files /dev/fd/63 and /dev/fd/62 are identical

$> diff -sq <(echo grep) <(echo notgrep)
Files /dev/fd/63 and /dev/fd/62 differ

   # See for yourself:
$> echo grep
$> echo \grep
$> echo \g\r\e\p  # just to really emphasize that there is no difference

Basically, the slash has no effect besides fooling the alias matcher.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 6

Trending Articles