[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: redirection to /dev/null
From: |
Eli Schwartz |
Subject: |
Re: redirection to /dev/null |
Date: |
Wed, 9 Sep 2020 21:02:57 -0400 |
On 9/9/20 8:16 PM, Koichi Murase wrote:
> 2020-09-10 8:04 Chris Elvidge <celvidge001@gmail.com>:
>> I've recently come across a crontab line:
>> 0 */6 * * * /usr/bin/modprobed-db store &> /dev/null
>
> `&>' is an extension to the POSIX shell, and the default setting of
> cron doesn't necessarily contain SHELL=/bin/bash. For example, Ubuntu
> by default seems to use /bin/sh (dash) to run crontab. If you want to
> use `&>' in crontab, you need to make it sure that "SHELL=/bin/bash"
> is set in the crontab.
>
>> I've also read that '&> /dev/null' is outdated and should be replaced
>> with '>/dev/null 2>&1'
>>
>> True? And exactly why?
>
> I'm also interested in whether it is really outdated and should not be
> used.
>
> I can find some discussion on the first item on the page
> https://wiki.bash-hackers.org/scripting/obsolete . The page says that
> it is because `&>' has a different unintended meaning in POSIX shell,
> but I think the discussion doesn't apply when one consciously writes
> Bash scripts (but not POSIX scripts). If you cannot use any
> constructs that have a different meaning in the POSIX shell, you can
> never use arithmetic commands `((...))' for example.
>
> I don't know other sources of the recommendation. AFAIK, the Bash
> manual doesn't mark it as deprecated nor obsolete. Bash even added a
> new variant `&>>' in version 4.0, so I guess they are not considered
> outdated by the developer side.
The construct is identical in behavior to standard POSIX syntax, and
saves 4 bytes in the user's highly space-constrained file (lmao).
I don't avoid bash syntax which misbehaves in POSIX due to having
different meanings. But, personally, I would say not to use &> or &>>
due to it having no compelling advantage, but spreading the google cargo
cult of using it in #!/bin/sh scripts due to people never being taught
the difference. And it is a pointless context switch if you need to keep
track of one more thing you cannot use in #!/bin/sh scripts. Much easier
to always remember "the canonical way to redirect stdout/stderr in shell
scripts of all kinds, is >/dev/null 2>&1"
I have a similar objection to the use of "==" in [[ which IMHO is just
asking for trouble and leads to way too many scripts using [ == ] (which
bash also accepts, and I find more troubling than accepting the
pointless alias in [[ but I don't make the rules...)
--
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User
signature.asc
Description: OpenPGP digital signature