Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> We can simplify this using control operators:

    [ -r ~/.profile ] && . ~/.profile
> Only if the readability check expression is truthful/succeeds, we want to source the file, so we use the && operator.

Almost. The real way to do this is to check for the non-existence of the file as the “success” case and do the action via a || on failure.

Otherwise if you run in a strict mode with error on any unhandled non-zero command (set -e), you’ll exit the script with failure when the profile doesn’t exist:

    [[ ! -r ~/.profile ]] || . ~/.profile
Note that the if expression does not have this issue as they don’t trigger the error on exit handling. Only the && approach does.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: