I created a directory named "$pattern" and now when ever i try to remove it, it says

pattern: Undefined variable.

I have tried:

$ rm -r $pattern
$ rm -rf $pattern
$ rm "$ option[value='2016']"

$, space, ' and [ are special characters in most shells. To remove their special meaning, you have to use the quoting mechanisms of the shell.

The quoting syntax varies very much with the shell.

In all shells that I know, you can use single quotes to quote all characters but single quote, backslash and newline (in Bourne-like shells, it does quote the last two as well, except in backticks for \ in some).

rm -r '$pattern'

Should work in most common shells.

rm -r \$pattern

Would work (except inside backticks for Bourne-like ones) in all shells but those of the rc family.

Same for:

rm "\$option[value='2016']"

In rc-like shells, you'd use:

rm '$option[value=''2016'']'

If directory is empty:

rmdir \$pattern


rm -r \$pattern

(It will delete recursevely any file that the folder contains)


When you need to operate on a file which contains special characters, an option is to use the bash autocomplete feature: start typing your command (in this case rmdir) and then hit Tab several times. This will cycle around all the files/directory in the current directory, automatically escaping all special characters.

This also works well if you're operating on very long filenames.


If all else fails, you can delete the file based on the inode number. First list all the files in the current directory with the inode numbers


then using the first number of the printout for your corresponding file name, you can use the following command to delete based on the inode number

find . -inum [inode-number] -exec rm -i {} \;


$ ls -il
total 0
804743 -rw-r--r--  1 staff  0 Nov  2 19:49 test1
804744 -rw-r--r--  1 staff  0 Nov  2 19:49 test2

$ find . -inum 804743 -exec rm -i {} \;
remove ./test1? y

$ ls -il
total 0
804744 -rw-r--r--  1  staff  0 Nov  2 19:49 test2

You simply escape the character using a "\", this is also a common practice used in writing SQL queries in web applications to prevent execution of database commands other than the ones intended.

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