Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1107957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:00:36+00:00 2026-05-17T02:00:36+00:00

I have defined the following variable: myVar=true now I’d like to run something along

  • 0

I have defined the following variable:

myVar=true

now I’d like to run something along the lines of this:

if [ myVar ]
then
    echo "true"
else
    echo "false"
fi

The above code does work, but if I try to set

myVar=false

it will still output true.
What might be the problem?

edit: I know I can do something of the form

if [ "$myVar" = "true" ]; then ...

but it is kinda awkward.

Thanks

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:00:37+00:00Added an answer on May 17, 2026 at 2:00 am

    bash doesn’t know boolean variables, nor does test (which is what gets called when you use [).

    A solution would be:

    if $myVar ; then ... ; fi
    

    because true and false are commands that return 0 or 1 respectively which is what if expects.

    Note that the values are “swapped”. The command after if must return 0 on success while 0 means “false” in most programming languages.

    SECURITY WARNING: This works because BASH expands the variable, then tries to execute the result as a command! Make sure the variable can’t contain malicious code like rm -rf /

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined the following variable var result = from report in firoozehDataContext.Reports select
I have the following variable defined: @property (nonatomic, retain) NSMutableArray *arraySpeechSentences; And I am
I have the following variable defined in a PHP document I am working with,
I have defined some models like this (Entity Framework Code-First): public class A {
I have the following defined: #!/usr/bin/env ruby class Something def self._attr_accessor key, value, type
There's probably a silly error in my code. I have defined the following variables:
I have defined following control template for my custom control. <ControlTemplate TargetType={x:Type local:CustomControl}> <Grid
I have defined following in a ResourceDictionary: <DockPanel x:Key=errorDisplay LastChildFill=False> <Border Background=Red DockPanel.Dock=Top> <TextBlock
In my .htaccess file I have defined following rule, RewriteRule t/([^.]+)/$ /videos/tag.php?tag=$1 [QSA] The
I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.