- as a filename to mean stdin/stdout is a convention that a lot of programs use. It is not a special property of the filename. The kernel does not recognise
- as special so any system calls referring to
- as a filename will use
- literally as the filename.
With bash redirection,
- is not recognised as a special filename, so bash will use that as the literal filename.
cat sees the string
- as a filename, it treats it as a synonym for stdin. To get around this, you need to alter the string that
cat sees in such a way that it still refers to a file called
-. The usual way of doing this is to prefix the filename with a path –
/home/Tim/-. This technique is also used to get around similar issues where command line options clash with filenames, so a file referred to as
./-e does not appear as the
-e command line option to a program, for example.
echo hello > -, you can use
echo hello > /dev/stdout.
While ‘-‘ is a convention that has to be implemented by each program wanting to support it,
/dev/stderrare, when supported by the OS (at least Solaris, Linux and BSDs do), independent of the application and then will work as you intend.
As camh mentioned,
- is just a naming convention used by some programs. If you want to refer to these streams with a file descriptor the shell will recognize, jiliagre was correct in having you use the name
/dev/stdout instead. Those file names should work any place a normal file name would work.
That being said, your first example is kind of silly. Any output that would be caught by the redirect operator to write to a file is already ON standard-output, so redirecting it and writing it back to where it came from is useless. The behavior you use there is the pipe, not a redirect:
echo hello |
In your second example you simply need to give can some indication that you want a litteral file of that name, not the internal alias it has. You can do this easiest by specifying a path to the file like this: