Hiro
Posts: 78
Joined: Fri Dec 30, 2011 11:09 am

alias problem

Tue Nov 27, 2012 7:38 pm

Being a lazy typist i tend to use alias quite a lot but there is one alias i wish to set that i cannot get to work. I often forget to sudo a command so i end up using sudo !! but am unable to set up an alias for this.
When i type

Code: Select all

alias sud="sudo !!"

I get the reply

Code: Select all

alias sud="sudo alias"
and sud is set to "sudo alias" which strangely gives a command not found error, as does typing "sudo alias" in the terminal which seems strange.
Has anyone managed this or am i the only one forgetful enough to need it? :oops:
Cheers, Hiro.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: alias problem

Tue Nov 27, 2012 7:59 pm

I think it will work if you use single quotes instead of double quotes.

Because in bash, ! is not special inside of single quotes (which is sensible) - unlike (t)csh where it is always special unless escaped (with \).
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Hiro
Posts: 78
Joined: Fri Dec 30, 2011 11:09 am

Re: alias problem

Tue Nov 27, 2012 8:57 pm

Thanks for the reply Joe but that doesn't seem to work either, alias now looks good

Code: Select all

alias sud='sudo !!'
However the error i get is

Code: Select all

sudo: !! command not found
I know a (very) tiny bit about escape characters and had already tried:

Code: Select all

 alias sud="sudo\!!"   
 alias sud="sudo \!!"
Have now tried same code but with single quotes but that didn't work either, possibly as my use of escape characters is wrong?
Also, i was trying over ssh and wondered about dodgy character recognition on the Windows machine i was using so i fired up the monitor and tried that but still no joy.
It's not the end of the world if i can't get it to work but i do hate being beaten.

User avatar
jojopi
Posts: 3135
Joined: Tue Oct 11, 2011 8:38 pm

Re: alias problem

Tue Nov 27, 2012 9:56 pm

History expansion does not take place on the results of alias expansion. So any "!"s in the alias are either expanded when the alias is set, or taken literally when the alias is used.

The only way around it is to avoid syntactical history expansion and instead use the built-in "history" command to obtain the previous command:

Code: Select all

alias sud='sudo $(history -p !!)'
Unfortunately, this loses any quoting that was present in the command, so commands with quotes will not repeat correctly:

Code: Select all

[email protected] ~ $ alias sud='sudo $(history -p !!)'
[email protected] ~ $ echo hello
hello
[email protected] ~ $ sud
hello
[email protected] ~ $ python -c 'print "hello"'
hello
[email protected] ~ $ sud
  File "<string>", line 1
    'print
         ^
SyntaxError: EOL while scanning string literal
Incidentally, the reason that "sudo alias" does not work is that alias is a shell built-in, and there is no external program "alias" that sudo could run with elevated privileges. (And even if there was, the alias would not affect your existing shell.)

It is also worth mentioning that a pipeline "A |B" repeated with "sudo !!" gives "sudo A |B", which runs A with privilege and B without, which may or may not be useful. Similarly a list command "A && B". And a compound command like "(A)" is not even syntactically correct when preceded by sudo. Redirections "A >FILE" are another case that cause confusion, because the redirection is set up by the shell before sudo even starts.

For all these reasons, it is extremely difficult to automate the desired effect of "repeat the last command as if it had been executed by a privileged user". You really need to understand the issues and consider the actual command to decide whether "sudo !!" will do or not. A naïve alias will hide this subtlety and work in even fewer cases, so I do not think it is well advised.

sdjf
Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: alias problem

Tue Nov 27, 2012 10:14 pm

Hiro wrote: When i type

Code: Select all

alias sud="sudo !!"

I get the reply

Code: Select all

alias sud="sudo alias"
and sud is set to "sudo alias" which strangely gives a command not found error, as does typing "sudo alias" in the terminal which seems strange.
As someone else has mentioned, alias is not a command, so it makes perfect sense that entering "sudo alias" or "sud alias" would give you a "command not found" error. aliasing is a bash "built-in".

But the use of !! is a new one on me, have never seen that before. Is it a more recent addition to command line tools? I know about tab expansion, but enter a real tab when I want to use it.
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

Hiro
Posts: 78
Joined: Fri Dec 30, 2011 11:09 am

Re: alias problem

Wed Nov 28, 2012 8:55 pm

@jojopi That code works well for me, it is only for when i do something daft like type out a long command, for example mounting a network drive without remembering the sudo. I know that sudo !! is only 7 keystrokes but like i said i'm a lazy typist. Incidentally the other route of up-cursor, home and then sudo and space is also 7 keystrokes:).

I had never heard of built-ins before so had to have a quick google and learnt a lot. I found a full list of bash built-ins (there aren't really that many) and did indeed discover that you cannot sudo any of them. I also managed to find out that;

Code: Select all

type alias
will inform me if alias (or insert any other command after type) is built-in or not.

It's always nice to learn something new so thanks for all your help,Hiro.

Return to “Beginners”