Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When trying to open a file with sudo gedit /path/to/file with a file name starting with a ! in front eg !config.ini i can not open the file. However if i do sudo nautilus then open the file with gedit it opens.If the file is renamed config.ini sudo gedit again works.Could someone explain this to me please.

share|improve this question
3  
Tip: Never use sudo with GUI software. use gksu or kdesudo (in KDE). –  Uri Herrera Aug 3 '12 at 23:22

4 Answers 4

up vote 6 down vote accepted

You are entering the sudo gedit /path/to/file command into a shell (by default bash) but modern shells use the '!' character for special purposes. Special characters can be interpreted without special meaning by preceding them with the '\' character.

Try:

gksu gedit /path/to/\!file

instead of

gksu gedit /path/to/!file

In modern shells the commands you enter are remembered and you can reenter them by using the '!' character. !! for example reenters the last command. As Uri points out, this is called an event designator. The bash interpreter also let's you use Ctrl-R to easily find commands you want to reenter, but the '!' mechanism is still there for us old-timers who have the habit of using '!' instead. Other uses of "!" include the $! parameter, and its use in arithmetic and logical expressions.

A bash reference may be helpful if you run into such mysteries because shells can be very complicated. The gnu bash manual is here.

Uri helpfully mentioned that you should use gksu or gksudo rather than sudo. There is an explanation here: What is the difference between "gksudo nautilus" and "sudo nautilus"?.

share|improve this answer
    
Thanks for the quick reply and alternative way to open the !file with the gksu gedit /path/to/\!file option –  damien Aug 3 '12 at 23:55
1  
@damien History expansion is a bit too crazy for my liking, so I disable it by putting set +H in my ~/.bashrc. Unless you really want to use history expansion, I recommend doing the same. –  geirha Aug 4 '12 at 11:28
    
@geirha, sounds like a good alternative. Would you mind adding it as an answer? –  John S Gruber Aug 4 '12 at 15:10

The easy fix is to disable history expansion permanently by adding the following line to your ~/.bashrc

set +H

If you've never intentionally used history expansion, you won't miss out on anything by disabling it.

share|improve this answer

It's an Event designator.

see man bash:

  • ! Expands to the process ID of the most recently executed back‐ ground (asynchronous) command.
share|improve this answer
    
Thankyou i found both answers helpful but marked the other correct for giving me an alternative way to still open the file from terminal –  damien Aug 3 '12 at 23:54
    
The man page can be pretty hard to follow, particularly that part. That quote is from the parameter section and it's therefore $! that expands to that process ID. Standing alone it is an Event Designator, as you mentioned. Thanks for the helpful correction you made to my answer, by the way. –  John S Gruber Aug 4 '12 at 15:18

A complete solution would be to put the directory name within single quotation marks (').

Example

sudo gedit /media/alexandro/'Disco Local'/'Dropbox (Equipe Alexandro)'/Pessoal/'!A Organizar'/Documentos/'!asd.txt'

share|improve this answer
    
Bem vindo ao AskUbuntu. Aqui só são aceites perguntas e respostas em inglês. Deves também evitar comentários que não tenham relevância para a solução. Convido-te a ler o guia do AskUbuntu. –  Luís de Sousa May 13 at 11:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.