Skip to content

Use (( )) instead of [[ ]]? #1

Description

@dimitry-ishenko

(1) I see bash-utility heavily using

[[ $# -lt 2 ]] && ...

Wouldn't it be more readable to do

(( $# < 2 )) && ...

instead? (( is used for arithmetic evaluation, which is what -lt is trying to force inside [[.

(2) Also, seeing this:

[[ $# = 0 ]] && ...

rubs me the wrong way on multiple levels.

First, we are all taught that = is assignment and == is equivalence comparison. Yes, in bash they are the same when used inside [[, but = is used for assignment elsewhere in bash and IMHO this is just a bad coding practice.

Second, operator = within [[ is used for pattern matching and not arithmetic comparison. So, it should minimally be:

[[ $# -eq 0 ]] && ...

or better yet:

(( $# == 0 )) && ...

to be more readable and consistent with (1).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions