Timeline for How do I check if a variable exists in an 'if' statement?
Current License: CC BY-SA 3.0
12 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 6, 2022 at 17:08 | comment | added | Kyle Strand | @Jupiter Yeah, that should be equivalent, I think, though it may need quotes around the expansion. | |
| Apr 6, 2022 at 9:13 | comment | added | Jupiter | @KyleStrand I usually do that with [[ -n ${MY_VAR:-} ]] | |
| Jan 8, 2019 at 22:56 | comment | added | Kyle Strand |
+1; this appears to be the simplest way to do this when set -u is in effect and the Bash version is pre-4.2.
|
|
| Oct 17, 2018 at 12:40 | comment | added | Tom Hale | Sorry @mikeserv, I have no idea what you mean... would you mind to try again with more simple examples? | |
| Oct 16, 2018 at 19:23 | comment | added | mikeserv |
@Tom Hale - to my my way of thinking, [ -z, if applied at all to a parameter check, is most useful as a corner case standin. the not applicable corollary... [ ${*?cant handle that, bash? or do the prior bournes slip at :? theres a test for that, too, if your word is worth the encoding... save for later? freezers are cool} and plus works for almost anything (stay positive, everybody) [ ${-+"-z"} $- ]&& now what? of course, by design, file system glo[bal]{1,2}ing might not be so confusing as a possible forty-two deep link back check... fortuitous? thats quite a lot.
|
|
| Oct 10, 2018 at 14:14 | comment | added | Tom Hale | @mikeserv I guess your yes is to my 2nd question... Is the first a yes, too? | |
| Oct 10, 2018 at 6:02 | comment | added | mikeserv |
@TomHale - yes, in rare cases. the [ test routines accept command line parameters, and so the usual expansions and interpretations as ordered in the usual way should be relied upon to render at invocation of the test applied that which you should cause to be read thereby by any program command line. test{!+"!"}
|
|
| Sep 3, 2018 at 14:19 | comment | added | Tom Hale |
@mikeserv Thanks, I like learning about edge cases :) Do you mean if [ -z "${somevar+x}" ]? Would the quoting still be required inside [[ and ]]?
|
|
| May 23, 2017 at 12:40 | history | edited | CommunityBot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
| Dec 1, 2015 at 1:30 | review | Suggested edits | |||
| Dec 1, 2015 at 3:06 | |||||
| Jun 26, 2015 at 0:02 | comment | added | mikeserv |
You need to quote $somevar to handle IFS=x. Either that or quote x.
|
|
| Jun 25, 2015 at 16:31 | history | answered | shivams | CC BY-SA 3.0 |