New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash's local keyword misconception #1092

Closed
adrelanos opened this Issue Jul 29, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@adrelanos
Member

adrelanos commented Jul 29, 2015

There is a common misconception about bash's local keyword. I think in #1089 (comment) it's at work.

    #local code=$(declare -p "$array")  # Original line; now replaced with line below
    local code=$(declare -p "$array" 2> /dev/null || true)  # This was line causing error

local will always return 0. Yes. That's it. Code talks...

script:

#!/bin/bash
set -x
set -e
test_function() {
   local somevar=$(this-fails)
   true "does not matter, script goes on"
}
test_function

output:

+ set -e
+ test_function
++ this-fails
./a: line 5: this-fails: command not found
+ local somevar=
+ true 'does not matter, script goes on'

If you preferred it fail, you could not write local and the command in question int the same line.

local somevar
somevar=$(this-fails)

In the example above it probably doesn't matter either way. My point is, if you are using local elsewhere, where non-zero exit codes matter, consider fixing this. That's all. Once agreed on this, this is closeable, unless you think this needs further work.

@nrgaway

@unman

This comment has been minimized.

Show comment
Hide comment
@unman

unman Jul 31, 2015

Member

I've reviewed all bash files under marmarek - I don't see one where this is an issue.
Suggest close.

Member

unman commented Jul 31, 2015

I've reviewed all bash files under marmarek - I don't see one where this is an issue.
Suggest close.

@nrgaway

This comment has been minimized.

Show comment
Hide comment
@nrgaway

nrgaway Jul 31, 2015

Interesting. I was wondering why that statement did not fail; that's the reason I mentioned it. In that case I would not want it to fail anyway.

Thanks for the heads up!

nrgaway commented Jul 31, 2015

Interesting. I was wondering why that statement did not fail; that's the reason I mentioned it. In that case I would not want it to fail anyway.

Thanks for the heads up!

@adrelanos adrelanos closed this Jul 31, 2015

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