4

This question already has an answer here:

Here is my ls command, which shows the list of files in my current directory:

rper:  ls -l

$commandoutput[0]  
file.txt

I tried to remove $commandoutput[0] line by using rm -rf $commandoutput[0], but it shows the following error. How can I remove it?

rper: rm -rd $commandoutput[0]
commandoutput: Undefined variable.

marked as duplicate by Gilles linux Jul 7 '17 at 23:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 7
    If you are new to the shell and have filenames with "special characters" (like $, * whitespaces, newlines and so on), it's generally a good idea to quote them: rm -rd '$commandoutput[0]' to avoid nay shell "magic" – Philippos Jul 7 '17 at 7:50
  • 2
    @Philippos : it is what should be used in the command line. And then he should look for which script used a : > '$commandoutput[0]' as well – Olivier Dulac Jul 7 '17 at 15:11
20

Escape $ (and desirably [, ]) sign in your file name with backslash \:

rm -rf \$commandoutput\[0\]
  • 5
    (also, in many shells tab completion will do that escaping for you once you're far enough to complete, which in this case means at least beyond the \$.) – Ulrich Schwarz Jul 7 '17 at 7:51
  • 1
    @UlrichSchwarz and, in zsh with setopt complete_in_word, you can tab-complete from anywhere in the filename, so something like comm<TAB> would work, no need to even type \$! – marcelm Jul 7 '17 at 15:39
  • honestly, that's the thing I can't understand - why so primitive answers gained so many upvotes? I wish more complex solutions gained many upvotes as well (a lot more deserving than this case) – RomanPerekhrest Jul 7 '17 at 20:52
27

In alternative to @RomanPerekhrest's answer, this will also work:

rm '$commandoutput[0]'  

as the single quotes will avoid variable expansion.

Another way is to start typing rm $ and then hitting Tab; the shell will autocomplete the filename, escaping characters as needed.

  • 7
    rm $<tab> tries to autocomplete with variable names for me, in bash and zsh. I need to escape the $ first, i.e. rm \$<tab> or rm '$<tab>. – Sparhawk Jul 7 '17 at 12:20
  • 4
    It's better to use Alt+/ instead of Tab — then the autocompletion will work only on file names. – Ruslan Jul 7 '17 at 18:33
1

While others have answered this question, another way to do this would be by finding the inode of the file and using find and exec delete to remove the file.

ls -il

Sample Output:

781956 drwx------  3 viv viv 4096 2017-07-7 15:05 $commandoutput[0] 

The first column should be the inode, in the above example, run:

find . -inum 781956 -exec rm -i {} \;
0

Another simple way is to use a wildcard and rm -i:

rm -i *command*

This will, for all the filenames containing the word "command", ask you if you want to remove the file. You can then check before deletion.

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