Skip to content

Add new check for error ignoring on command substitutions #2338

Description

@felipecrs

For new checks and feature suggestions

Suggestion

Given the following script:

#!/bin/bash

set -e

basename "$(curl -fsSL -w '%{url_effective}' -o /dev/null https://github.com/docker/compose/releases/latest)"

It would normally work, but curl could actually fail (such as with command not found). So, for the sake of the example, I will force it to fail by using curl2.

Examples:

$ bash <<'EOF'; echo $?
#!/bin/bash

set -e

basename "$(curl -fsSL -w '%{url_effective}' -o /dev/null https://github.com/docker/compose/releases/latest)"
EOF
v2.0.0
0

With curl2:

$ bash <<'EOF'; echo $?
#!/bin/bash

set -e

basename "$(curl2 -fsSL -w '%{url_effective}' -o /dev/null https://github.com/docker/compose/releases/latest)"
EOF
bash: line 5: curl2: command not found

0

The status code of the latter example is 0 even if the command failed. This is misleading. ShellCheck could advise to be written like so instead:

$ bash <<'EOF'; echo $?
#!/bin/bash

set -e

curl_output="$(curl2 -fsSL -w '%{url_effective}' -o /dev/null https://github.com/docker/compose/releases/latest)"
basename "$curl_output"
EOF
bash: line 5: curl2: command not found
127

References twpayne/chezmoi#1466 (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