I have had these issues for a while now. I've tried different approaches, all to no avail.
-
4What have you tried and what errors are you getting? Are you really trying to create a file with 12 backslashes in the name? If not, what is the real name of the file you're trying to create? – doneal24 Sep 06 '22 at 13:51
-
Quoting correctly (single quotes), none of the backslash is a problem. @doneal24 – QuartzCristal Sep 06 '22 at 14:55
-
4@QuartzCristal You are right of course but that doesn't really address my comment. Questions generally should provide what the poster has already tried so they can learn and we don't duplicate their efforts. – doneal24 Sep 06 '22 at 15:17
-
Yes, please [edit] your question and show us what the expected file name is. – terdon Sep 06 '22 at 16:04
5 Answers
Simply
touch "\\\*\\\\'\"Best School\"\\'\\\\\*\$\\\?\\\*\\\*\\\*\\\*\\\*:)"
The file-name suggests that you are aware of the use of quotes and backslashes, so it should have been easy. But rethink your process/workflow, because you're much better of with normal names.

- 4,643
Entering using a here-document (where the delimiter, here .
is quoted so that the contents of the here-doc be taken literally) makes it easier:
xargs -d'\n' touch -- << '.'
\*\\'"Best School"\'\\*$\?\*\*\*\*\*:)
.
(here assuming GNU xargs
) or:
touch -- "$(cat << '.'
\*\\'"Best School"\'\\*$\?\*\*\*\*\*:)
.
)"
With ksh
or zsh
,
touch -- "$(<< '.'
\*\\'"Best School"\'\\*$\?\*\*\*\*\*:)
.
)"
Also works, and in mksh
that's effectively treated as a form of multi-line quote as an optimisation. See Understanding Bash's Read-a-File Command Substitution for details.
With the rc
shell or derivatives or zsh
after set -o rcquotes
:
touch '\*\\''"Best School"\''\\*$\?\*\*\*\*\*:)'
There, you just need to wrap the name in single quotes, and escape the single quotes as ''
within.

- 544,893
In a sh
-compatible shell, like bash
, single-quote the whole string, then replace each internal single quote with '\''
(or with '"'"'
):
touch -- '\*\\'\''"Best School"\'\''\\*$\?\*\*\*\*\*:)'
Each internal sequence of '\''
temporarily breaks out of the outer single-quoted string, inserts an escaped single quote, and then continues the single-quoted string.
This is necessary since a single-quoted string can't contain single quotes.
You could also use a quoted here-document:
touch -- "$(cat <<'END_NAME'
\*\\'"Best School"\'\\*$\?\*\*\*\*\*:)
END_NAME
)"
This uses cat
to pass on a string from a here-document redirection. The document, a single line in this instance, is passed on as the result of the command substitution without the shell interfering in its contents, as the quoting of the initial document delimiter ('END_NAME'
) prevents it from acting on expansions.
(There are no valid expansions in our example string anyway, but quoting the here-document would avoid expanding things that look like variables or command substitutions in generic strings).

- 333,661
fill a file with desired filename.
cat > se.txt
\*\\'"Best School"\'\\*$\?\*\*\*\*\*:)
CTRL-d
then touch it
touch "$(< se.txt )"

- 31,554
Make sure you have a variable that prints the correct filename. Then touch with the variable. You can printf as many times as needed to get the file name right before you use the touch
command.
$ var='\*\\'\''"Best School"\'\''\\*$\?\*\*\*\*\*:)'
$ printf '%s\n' "$var"
*\'"Best School"'\*$?*****:)
$ touch "$var"
Or perhaps:
$ var=$'\*\\\\\'"Best School"\\\'\\\\*$\\?\\*\\*\\*\\*\\*:)';echo "$var"
\*\\'"Best School"\'\\*$\?\*\*\*\*\*:)
Or any other way to get the correct value inside the variable.

- 2,023