Jay Taylor's notes
back to listing indexTest if a variable is set in bash when using "set -o nounset"
[web search]
The following code exits with a unbound variable error. How to fix this, while still using the
|
||||
In this case, |
|||||||||||||||||||||||||||
|
You need to quote the variables if you want to get the result you expect:
Test:
|
|||||||||||||||||||||||||||
|
If you want a non-interactive script to print an error and exit if a variable is null or not set:
If you don't want the script to exit:
You can even use Note what the colon does above. From the docs:
There is apparently no need for In summary, I may typically just use |
||||
How about a oneliner?
|
|||||||||
|
You can use
but
might be more readable. |
|||||||||
|
Your Answer
Not the answer you're looking for? Browse other questions tagged bash shell unix or ask your own question.
asked |
4 years ago |
viewed |
14419 times |
active |
Related
Hot Network Questions
- A word for an inescapable and pointless situation?
- Is it possible to travel to the US to speak at a conference under the rules of VWP?
- At what step is the difference made between a quesadilla and burrito?
- How can I explain to my parents that I study programming languages?
- How to create nonbreaking arbitrary length snakes for full-width justification?
- I don't want to kill any more mice, but my advisor insists that I must in order to get my PhD
- How to properly write an anagram efficiently in c#
- do(x) operator meaning?
- How to obtain this expression in terms of real functions
- My son is being hit by a friend, and we need to discuss the situation with his mom
- Mint is becoming more withered
- How to debug math?
- Is it plagiarism when copying something that you have written before?
- How could both Occam's Razor and complex theories like Quantum Mechanics be correct at the same time?
- What's the smallest change to physics required to allow magic?
- Counting in bijective base 62
- When should I extend a Java core class
- How do I say that I am interested in a lot of things without coming off either as superficial or arrogant
- The price dropped on an item I purchased 8 days ago. What can I do?
- Do we know where Ridley Scott got the idea for the xenomorph inner-mouth?
- If vibranium is the strongest metal on earth, then why did Captain America's shield dent when Peggy Carter shot it?
- Turning a celebrity into a non-person?
- What is the pH of ice?
- Can I put new wrist watch in checked or carry-on bag?
Technology | Life / Arts | Culture / Recreation | Science | Other | ||
---|---|---|---|---|---|---|