0

I need to create a file with this exact name "\?$*'ChouMi'*$?\" so that ls would show it as:

$ ls | cat -e
"\?$*'ChouMi'*$?\"$
$

1 Answer 1

3

Put the name in single quotes. This makes the shell not process backslashes or things that look like expansions in the string.

For each embedded single quote, temporarily break out of the single-quoted string and add a quoted single quote (this essentially means replacing each single quote with '\'' or '"'"' on the command line):

$ touch '"\?$*'\''ChouMi'\''*$?\"'
$ ls
"\?$*'ChouMi'*$?\"

Similarily if you need the name in a variable:

$ name='"\?$*'\''ChouMi'\''*$?\"'
$ touch "$name"
$ ls
"\?$*'ChouMi'*$?\"

(Note that the quoting of $name whenever that value is used is essential.)

Somewhat related:

1
  • '\'' is what also I have contructed, about 25 years ago... :-) I think, '"'" is much lesser popular because our intuition dictates that "'" is always somehow "stronger" as """.
    – peterh
    Commented Jan 9 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .