Skip to main content

Timeline for Testing if shell variable is set

Current License: CC BY-SA 4.0

14 events
when toggle format what by license comment
May 9, 2023 at 8:19 comment added Edgar Magallon @ilkkachu And I just realized that the relevant text is above the examples haha :). Also, thanks for the link, it's more useful and handy!
May 9, 2023 at 8:14 history edited ilkkachu CC BY-SA 4.0
added 2 characters in body
May 9, 2023 at 7:54 history edited DaG CC BY-SA 4.0
added 188 characters in body
May 9, 2023 at 7:45 comment added DaG Sorry for misunderstanding the question. I thought that you want to test if variable is set not if it's empty...
May 9, 2023 at 7:39 comment added ilkkachu @EdgarMagallon, yep, one often sees just the ones with the colon. Sadly, Bash's manual, hides the distinction within the text, it says "Omitting the colon results in a test only for a parameter that is unset. [...] if the colon is omitted, the operator tests only for existence." but all the examples are with the colon. The POSIX text has a nice table of all the variants, though, so at least here, it's a better read.
May 9, 2023 at 7:00 comment added Edgar Magallon @ilkkachu oh, that's true! my bad. I really thought they missed the colons, I had no idea about that until I tried what you said. Thanks!
May 9, 2023 at 5:19 history edited ilkkachu CC BY-SA 4.0
deleted 16 characters in body
May 9, 2023 at 5:18 comment added ilkkachu @EdgarMagallon, oh, I guess my comment above is for you.
May 9, 2023 at 0:52 comment added ilkkachu With ${var:+x}, you get x only if the variable is set and non-empty. Without the colon ${var+x}, you get x is it's set to any value. Same with ${var:?error}, it'd also trigger for a set but empty value, not just an unset one.
May 9, 2023 at 0:47 history edited Edgar Magallon CC BY-SA 4.0
format, missing ":", remove unncesary/duplicated example: "${var:?error_message}"
May 9, 2023 at 0:41 history edited DaG CC BY-SA 4.0
deleted 10 characters in body
May 9, 2023 at 0:36 history undeleted DaG
May 9, 2023 at 0:35 history deleted DaG via Vote
May 9, 2023 at 0:34 history answered DaG CC BY-SA 4.0