blesius
blesius

Reputation: 53

Bash - aliasing unalias

I know that using alias command in bash can define custom functions in bash, and can even overwrite inbuilt functions (for e.g. alias sudo="help" works). Unalias -a can tidy up the mess made with alias command, but unalias can also be aliased (I have run alias unalias="unalias" command successfully, so I guess it works for other commands too - I did not want to mess up the os since it was a public computer).
My question is that if I alias the unalias to something else, and then the alias too, can I reset the commands if they are themselves not usable? What happens if I aliased sudo too?
For e.g. if I wanted to be a troll and run this command on the machine of a friend of mine

    alias sudo="clear" && alias unalias="clear" && alias alias="clear"

all these commands will be unusable, so I'm sure it will be a hard time to restore the functionality of sudo. Is it possible without reinstalling the OS?
(I can think about using ctrl+alt+f1 that switches to a terminal, but I do not know if the aliased stuff is aliased there too. What if I do the same thing there?)

Upvotes: 2

Views: 2108

Answers (3)

Andrea Corbellini
Andrea Corbellini

Reputation: 17751

To expand anubhava's answer: you can also use single or double quotes:

$ alias ps=date
$ ps
Wed Feb 18 12:33:17 EST 2015
$ "ps"
 PID TTY           TIME CMD
 615 ttys000    0:00.66 -bash
 621 ttys001    0:07.36 -bash
 913 ttys002    0:01.19 -bash

In general, any expression that evaluates to ps will be unaliased.

Upvotes: 2

anubhava
anubhava

Reputation: 785098

A commonly used technique to prefix a command with a backslash i.e. \ to make sure to avoid any aliases and run it from system default path:

Example:

$> alias ps=date
$> ps
Wed Feb 18 12:33:17 EST 2015
$> \ps
 PID TTY           TIME CMD
 615 ttys000    0:00.66 -bash
 621 ttys001    0:07.36 -bash
 913 ttys002    0:01.19 -bash

As you can use running \ps runs system default /bin/ps while running ps runs the aliased command date.

Upvotes: 8

hek2mgl
hek2mgl

Reputation: 157967

If you want to execute a binary and not an alias with the same name just type the whole path to the binary:

/usr/bin/sudo <cmd>

Having that aliases are only available in interactive shells and not in shell scripts the also won't break anything else.


To "remove" that aliases simply remove them from your .bashrc and start a new shell.

Upvotes: 1

Related Questions