Re: [dev] Shell style guide

From: Evan Gates <evan.gates_AT_gmail.com>
Date: Tue, 6 Sep 2016 13:14:59 -0700

On Tue, Sep 6, 2016 at 1:09 PM, Antenore Gatta <antenore_AT_simbiosi.org> wrote:
> Remember to quote whatever is inside the the substitute commands and
> that you can
> nest them:
>
> $(whatever "$foo" $(othercmd "$bar"))

almost, you'd need

"$(whatever "$foo" "$(othercmd "$bar")")"

as "$()" needs to be quoted as well


> This is sometimes true, but printf is not safe as well from that point
> of view,
> as it's a builtin as well in most shells.

It's not about builtin vs external, it's about surprises. Whether
printf if builtin or not, it will behave in a predictable way. Doing

var=-n
printf %s\\n "$var"
echo "$var"

printf will always give us the same result, echo may or may not
depending on shell and system.

> In scripts where you just need to output text, or to easily list files,
> echo is
> fine.
>
> echo *
> echo /path/*/whatever/*sh # that is better and faster than "ls"
> echo "I'm right"

And if you have a file named -n or -e? Don't get me wrong, echo has a
place, and interactive shell is is definitely one of those places. But
doing this in a script should be avoided.

> A flame maybe, what do you think about shellcheck? [1]
> If it sucks (IMO it doesn't at all), do we need a suckless version.
>
> [1] http://www.shellcheck.net/

I love shellcheck. It catches most common mistakes and can be
installed locally to run shellcheck myscript.
Received on Tue Sep 06 2016 - 22:14:59 CEST

This archive was generated by hypermail 2.3.0 : Tue Sep 06 2016 - 22:24:18 CEST