Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

like image 608
blesius Avatar asked Feb 26 '26 04:02

blesius


2 Answers

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.

like image 92
anubhava Avatar answered Feb 28 '26 16:02

anubhava


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.

like image 36
Andrea Corbellini Avatar answered Feb 28 '26 17:02

Andrea Corbellini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!