-2

I am trying to make a file with special characters. But I don't want to use double or single quotes.

How can I make the file ?

Archemar
  • 31,554
anuja
  • 1
  • Just use escaping (maybe with hex codes). – ridgy Jan 08 '18 at 13:44
  • 2
    Are you talking about the name? Or about the contents? What kind of special characters? Shell metacharacters? Non-Latin-1 characters? Control codes? You are talking about doing this from shell script, ne? Please [edit] your question to make all of these missing and ambiguous parts clear. – JdeBP Jan 08 '18 at 13:46
  • See also: https://unix.stackexchange.com/q/347484/117549 – Jeff Schaller Jan 08 '18 at 13:48
  • 4
    I'm curious why you can't use single or double-quotes? – Jeff Schaller Jan 08 '18 at 13:48

1 Answers1

2

Backslash provides with another form of quoting in Bourne-like shells, so you can do:

printf %s \$\ \; > file

For instance to have file contain those 3 ($, space and ;) special characters. You can also use octal sequences in printf to specify exact byte values:

printf \\1\\2 > file

Makes a file with bytes 1 and 2. Some printf implementations also support hex sequences:

printf \\xff\\x80 > file

Or specifying characters with their Unicode code point:

printf \\u20ac > file

For the euro sign for instance. In most implementations, the character will be output in the locale's character set. An exception is the printf builtin of ksh93 which outputs it in UTF-8 regardless of the locale's charset.

See also here documents for another way to enter text. That's convenient for multi-line text:

cat << \EOF > file
some text with $ \ | # *
and anything you want
EOF

To create file names with such characters, use:

echo text > \$\ \;

or:

echo text > $(printf \\1\\2)

etc. Note that as $(...) is subject to split+glob, you need to disable it with:

IFS=; set -o noglob

For characters that are otherwise in $IFS or glob operators (*, ?, [...]).