Skip to content
Fix Code Error

How can I declare and use Boolean variables in a shell script?

March 13, 2021 by Code Error
Posted By: Anonymous

I tried to declare a Boolean variable in a shell script using the following syntax:

variable=$false

variable=$true

Is this correct? Also, if I wanted to update that variable would I use the same syntax? Finally, is the following syntax for using Boolean variables as expressions correct?

if [ $variable ]

if [ !$variable ]

Solution

Revised Answer (Feb 12, 2014)

the_world_is_flat=true
# ...do something interesting...
if [ "$the_world_is_flat" = true ] ; then
    echo 'Be careful not to fall off!'
fi

Original Answer

Caveats: https://stackoverflow.com/a/21210966/89391

the_world_is_flat=true
# ...do something interesting...
if $the_world_is_flat ; then
    echo 'Be careful not to fall off!'
fi

From: Using boolean variables in Bash

The reason the original answer is included here is because the comments before the revision on Feb 12, 2014 pertain only to the original answer, and many of the comments are wrong when associated with the revised answer. For example, Dennis Williamson’s comment about bash builtin true on Jun 2, 2010 only applies to the original answer, not the revised.

Answered By: Anonymous

Related Articles

  • Summarize the data using two Python data Frames
  • Arrange date columns based on unique id column using R
  • Reference - What does this regex mean?
  • Creating a pandas DataFrame from columns of other…
  • What does this symbol mean in JavaScript?
  • What are the new features in C++17?
  • Reference — What does this symbol mean in PHP?
  • summing two columns in a pandas dataframe
  • How do the PHP equality (== double equals) and…
  • Why does my Spring Boot App always shutdown…
  • What is the worst programming language you ever worked with?
  • data.table vs dplyr: can one do something well the…
  • MySQL server has gone away - in exactly 60 seconds
  • Spring Boot application.properties value not populating
  • How to get $HOME directory of different user in bash script?
  • How to calculate means for multiple groups of rows…
  • PHP parse/syntax errors; and how to solve them
  • Vis.js - how to hide the gridlines
  • Pandas left outer join multiple dataframes on…
  • How to echo with different colors in the Windows…
  • Fastest way to list all primes below N
  • How do I get a list of all the duplicate items using…
  • Name [jdbc/mydb] is not bound in this Context
  • Left join only selected columns in R with the…
  • Calculate Pandas DataFrame Time Difference Between…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Explaining the 'find -mtime' command
  • Groupby of different columns with different…
  • Smart way to truncate long strings
  • how to return JSON object if search string matches…
  • Removing double quotes from variables in batch file…
  • count number of rows in a data frame in R based on group
  • How to permanently set $PATH on Linux/Unix?
  • What's a concise way to check that environment…
  • Vis.js timeline with polymer error : Something is…
  • Pandas groupby month and year
  • Tomcat started in Eclipse but unable to connect to…
  • Echo a blank (empty) line to the console from a…
  • Calculate a Running Total in SQL Server
  • Awk sort by last column and the print the whole line
  • First echo missing when using bash -c over SSH
  • Oracle listener not running and won't start
  • Problems with local variable scope. How to solve it?
  • javascript .replace and .trim not working in vuejs
  • VueJS bind model to dynamically inserted form element
  • How to use shell commands in Makefile
  • What is the scope of variables in JavaScript?
  • Why does C++ code for testing the Collatz conjecture…
  • MongoDB - admin user not authorized
  • how to convert date to a format `mm/dd/yyyy`
  • Backbone marionette composite view?
  • Java ElasticSearch None of the configured nodes are…
  • Oracle Sort of gaps and island query
  • What are the undocumented features and limitations…
  • Left Join without duplicate rows from left table
  • How to define partitioning of DataFrame?
  • Call action function when template is loaded (Ember.js)
  • Git - Pushing code to two remotes
  • Download multiple stocks with pandas yahoo finance…
  • How to display gpg key details without importing it?
  • Converting Oracle SQL Procedure into MySQL Stored Procedure
  • Classpath resource not found when running as jar
  • Why is ggplot geom_area empty when attempting to…
  • Understanding PrimeFaces process/update and JSF…
  • org.glassfish.jersey.servlet.ServletContainer…
  • How/When does Execute Shell mark a build as failure…
  • How to determine the current shell I'm working on
  • Codeigniter 4 hasChanged() - I was expecting no…
  • Defining user variables in stored procedures /…
  • Get the name of an object's type
  • How to set HTML5 required attribute in Javascript?
  • Why is reading lines from stdin much slower in C++…
  • Cursor inside LOOP is jumping out too early
  • Child Component not updating when parent updates vuejs
  • Spring Hibernate - Could not obtain…
  • Change the location of the ~ directory in a Windows…
  • SASS compiler not adding to include path
  • How to find out client ID of component for ajax…
  • TypeError: Cannot read property 'webpackJsonp' of undefined
  • How to Replace Multiple Characters in SQL?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Javascript code for showing yesterday's date and todays date
  • Peak signal detection in realtime timeseries data
  • Flex: REJECT rejects one character at a time?
  • Generate an array of years with duplicates of each…
  • How does PHP 'foreach' actually work?
  • SQL query return data from multiple tables
  • Check if current date is between two dates Oracle SQL
  • Aurelia bundling issue with virtual directory
  • Use placeholders in yaml
  • Unable to execute dex: method ID not in [0, 0xffff]: 65536
  • Unexpected end of JSON input while parsing
  • Bash: Echoing a echo command with a variable in bash
  • How to add fonts to create-react-app based projects?
  • Batch File: ( was unexpected at this time
  • SQL Server Trigger on Specified Columns
  • Using if(isset($_POST['submit'])) to not display…
  • Where and why do I have to put the "template" and…
  • Laravel Eloquent compare date from datetime field
  • How do i update a javascript variable as its value changes?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Sorting an array of objects by property values

Next Post:

Extracting extension from filename in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error