Hacker News new | ask | show | jobs
by mgdlbp 932 days ago
The first answer is wrong, so is the second's explanation... twice it conflates unset with null/empty. Very illustrative!

The substance of the first answer:

    #!/bin/bash
    
    # Check if the first parameter is not provided
    if [ -z "$1" ]; then
        echo "Error: Parameter not provided."
        exit 1
    fi
The snippet precedes the true statement:

> This checks if the first parameter (`$1`) is empty.

But what happened to the supplied task? It was stated and echoed as:

> Anonymous: ...if it's not passed?

> ChatGPT: ...if a specific parameter is not passed...

> ChatGPT: ...if the parameter is set.

The second answer:

    #!/bin/bash
    : ${1?"Error: Parameter not provided"}
This is correct. But the explanation is not:

> ...and the `${1?...}` part checks if the first positional parameter (`$1`) is unset or null.

--

(the most succinct possible idiom is `#!/bin/bash -u`)