I know that >& means redirect stderr to stdout.
But I don't know the meaning of >&!, I tried following command but it seems like normal.
Example:
cat xxx.txt >&! xxx.outputMy environment is csh shell and ubuntu18.
Could someone teach me this, thanks.
22 Answers
Whenever you have a question like this, the answer will almost certainly be in the manual. I just run man csh and then pressed / to search, entered &! and then enter. That took me to the relevant section:
> name
>! name
>& name
>&! nameThe file
nameis used as standard output. If the file does not exist then it is created; if the file exists, it is truncated, its previous contents being lost.If the shell variable
noclobberis set, then the file must not exist or be a character special file (e.g., a terminal ordev/null) or an error results. This helps prevent accidental destruction of files. In this case the!forms can be used to suppress this check. Ifnotemptyis given innoclobber,>is allowed on empty files; ifaskis set, an interactive confirmation is presented, rather than an error.The forms involving
&route the diagnostic output into the specified file as well as the standard output.nameis expanded in the same way as<input filenames are.
So, the quoted section explains csh's redirection operators. To summarize the above, > redirects to a file and >! forces the redirection to occur even if the user has set the noclobber option which normally blocks you from redirecting to an existing file. The & redirects standard error as well as standard output, so >&! will redirect both stderr and stdout to a file, overwriting any content the file may have even if the noclobber option is set.
Note, this answer is for bash, not for csh like OP added afterwards.
>& does not mean redirect stderr to stdout (which would be 2>&1), it means redirect stderr+stdout to something.
And in your case, ! is the something.
From GNU Bash Reference Manual >> Redirections
>> 3.6.4 Redirecting Standard Output and Standard Error:
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word.
There are two formats for redirecting standard output and standard error:
&>wordand
>&wordOf the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
So, &>! will redirect stdout+stderr to a file called !.
Note, that both versions are GNU bashisms, only the last version (>word 2>&1) is Posix compliant.
Also be careful, ! will invoke history expansion if something follows.