by
97 9
2
5
2,263
0
Top 1% !
Popular
Famous
Specified
OpenSource
Popularity: 7899th place

Published on:

No tags for this snippet yet.
Languagesh
LicenseMIT_X11

sh: avoid runtime error when a variable is supposed to contain a number but it is actually empty

Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/43859/sh--avoid-runtime-error-when-a-variable-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
myNum=''; [ ${myNum:-0} -eq 0 ] && echo ok # ok [ ${myNum:-1} -eq 1 ] && echo ok # ok [ ${myNum:-1} -gt 0 ] && echo ok # ok [ ${myNum:-0} -gt 1 ] || echo "0 is not greater than 1" # 0 is not greater than 1 # This behaviour comes from the common way to extract a substring from a # variable tmp='mississippi'; echo ${tmp:0:2} # mi echo ${tmp:1:1} # i # echo ${tmp:2} # ssissippi
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus