[ -f /tmp/myfile.txt ] && echo "File exists" || echo "No such file"
How does this work? Specifically the the evaluation of && and ||.
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.
&&and||are control operators that determine whether the following command should be executed based on the exit status of the prior command. Fromman (1) bash:[, ortest, returns a zero exit status if the command passes the test. Fromhelp test:So when you do
what you’re really saying is (read this carefully, please) If the file exists, echo foo. If the file does not exist or the
echo foocommand fails, echo bar.This is the subtle, but critical difference between these control operators and an
if…thencommand.