Boolean variable in a Shell Script

I'm writing a Linux Shell script and would like to have a Boolean variable called FileA_isPresent
I would like to set the variable to true if the file is present and false if not

Is this possible? I've used this in every other language I've used but looking on the net it seems that Linux shell scripts don't handle Boolean variables in the usual way?

Am I wrong? if so it would be great to get some pointers.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shell variables contain either numbers or strings, so you could store "true" and "false" in them, or 1 and 0.

You then test against the values as, for example, if $a is set to 0 (for false) or 1 (for true):
if [[ $a -eq 1 ]]; then
  # do something if $a is 1, for true
  # do something if $a is 0, for false

Open in new window

text versions are more obvious, so
if [[ $a == "true" ]]; then
 # Action if true
  # action if false

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
Not refreshed this page, before writing my comment. => to see comment from simon3270.

Yes it is possible.
Providing the comment base on:
>> I've used this in every other language
Sample ways:
/bin/ls -ld FileA_isPresent >/dev/null 2>&1
echo $FileA_isPresentValidateUsingDollarQuestion used to be zero if that file is present or non zero if not present

Open in new window

Same using if (learn if elif -
if [[ -f ./FileA_isPresent ]]
     echo "FileA_isPresent file is present"
echo SaveRet $SaveRet

Open in new window

Always better save $? using a variable.
$ /bin/ls -ld ./FileA_isPresent >/dev/null 2>&1
$ echo $?
$ echo $?
$# zero since previous echo $? worked.

Open in new window

You can view $? using:
export PS1='$LOGNAME@$HOSTNAME $PWD [ $? ]
$ '

Open in new window

You can test, as the example points, if the variable is defined it will be seen as try, or use true, 1

You need to provide a context to what it is you want the variable for.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.