The less than and symbol (<
) is opening the file up and attaching it to the standard input device handle of some application/program. But you haven't given the shell any application to attach the input to.
Example
These 2 examples do essentially the same thing but get their input in 2 slightly different manners.
opens file
$ cat blah.txt
hi
opens STDIN
$ cat < blah.txt
hi
Peeking behind the curtain
You can use strace
to see what's going on.
When we read from a file
open("blah.txt", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0664, st_size=3, ...}) = 0
fadvise64(3, 0, 0, POSIX_FADV_SEQUENTIAL) = 0
read(3, "hi\n", 65536) = 3
write(1, "hi\n", 3hi
) = 3
read(3, "", 65536) = 0
close(3) = 0
close(1) = 0
When we read from STDIN (identified as 0)
read(0, "hi\n", 65536) = 3
write(1, "hi\n", 3hi
) = 3
read(0, "", 65536) = 0
close(0) = 0
close(1) = 0
In the first example we can see that cat
opened the file and read from it, blah.txt
. In the second we can see that cat
reads the contents of the file blah.txt
via the STDIN file descriptor, identified as descriptor number 0.
read(0, "hi\n", 65536) = 3
Redirecting the standard error immediately to /dev/null
is a bad idea as it will hide early error messages, and failures may be hard to diagnostic. I suggest something like the following start-app
zsh script:
#!/usr/bin/env zsh
coproc "$@" 2>&1
quit=$(($(date +%s)+5))
nlines=0
while [[ $((nlines++)) -lt 10 ]] && read -p -t 5 line
do
[[ $(date +%s) -ge $quit ]] && break
printf "[%s] %s\n" "$(date +%T)" "$line"
done &
Just run it with: start-app your_command argument ...
This script will output at most 10 lines of messages and for at most 5 seconds. Note however that if the application crashes immediately (e.g. due to a segmentation fault), you won't see any error message. Of course, you can modify this script in various ways to do what you want...
Note: To make completions work with start-app
in zsh, it suffices to do:
compdef _precommand start-app
and in bash:
complete -F _command start-app
(copied from the one for exec
and time
in /usr/share/bash-completion/bash_completion
).
Best Answer
Just change the order and do the redirection of stderr to
/dev/null
FIRST:Keep in mind that redirections are always performed from left to right; the idea is to have the stderr already redicted when the failing redirection is performed.
However, AFAIK, only
zsh
allows you to use just<file
as an equivalent ofcat file
(ormore file
, subject to itsNULLCMD
andREADNULLCMD
options).While bash and some other shells deceptively support
$(<file)
as a special form of command substitution, they only support that exact form, nothing more:While in zsh: