Glam Prestige Journal

Bright entertainment trends with youth appeal.

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.output

My environment is csh shell and ubuntu18.

Could someone teach me this, thanks.

2

2 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
>&! name

The file name is 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 noclobber is set, then the file must not exist or be a character special file (e.g., a terminal or dev/null) or an error results. This helps prevent accidental destruction of files. In this case the ! forms can be used to suppress this check. If notempty is given in noclobber, > is allowed on empty files; if ask is 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. name is 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:

&>word

and

>&word

Of 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.

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy