Are the following two boolean expressions the same?
if [ -n $1 ] ; then
if [ -n "$1" ] ; then
And these two
if [ $? == 0 ] ; then
if [ "$?" == 0 ] ; then
If not – When should you put a variable in quotes?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
put variables in quotes when there is any chance that the value may contain white spaces, or in general NOT be a continuous string of characters. So
as
$?should always be something between 0 and 255, you don’t need to quote that because it is a return value set after each sub-process returns. It is NOT possible to break that by assigning a string value directly, i.e.because a user variable name must start with [A-Za-z_] , so don’t do that 😉
Whereas for $1, if a value is passed in like
the test
will blow up,
but the test
will succeed.
IHTH