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 3980262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:16:10+00:00 2026-05-20T05:16:10+00:00

From what I understand you can do C-style for and while loops in bash.

  • 0

From what I understand you can do C-style for and while loops in bash.

LIMIT=10

for ((a=1; a <= LIMIT ; a++))  # Double parentheses, and "LIMIT" with no "$".
do
  echo -n "$a "
done                           # A construct borrowed from 'ksh93'.

And even ternary operators.

(( var0 = var1<98?9:21 ))

How would you do this with the if statement?

Also why don’t they implement braces like in C? What is the logic with using all of these keywords like done, do, if, and fi? I will be writing some scripts but bash appears very different.

Are there any bash styling techniques or bash alternatives/plugins? I would like to follow the standard, but coming from a C, Java and PHP background bash looks really weird. Maintainability and standards are important.

  • 1 1 Answer
  • 0 Views
  • 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-20T05:16:11+00:00Added an answer on May 20, 2026 at 5:16 am

    There are some syntactic specialties of bash (and maybe other shells of the sh family) related to if:

    • The basic if-statement has the syntax if condition-command ; then list ; fi (with the possibility to add elif and else for other branches).

      • The condition-command can be any command that bash supports (even another if, I think), is executed and the return value is then evaluated. (Actually, it can also be a list of commands, but this might confuse readers. The return value of the last command counts for the condition.)
      • Bash (and Unix shells in general) interprets result codes of programs in the following way: a result of 0 is interpreted as true (or "no error"), and any other result is interpreted as false (or "some error occurred"). (In C and some other languages it is the other way around: 0 is false and everything else is true.)
      • If the result of the condition-command is 0 (true), the list is executed (and the return value of the if is the result of the list). If it is not-0 (false), the list is not executed (but the elif or else branches, if there are any). (If none of the lists is executed, the return value of the if is 0.)
    • Some useful commands as conditions (these work also as conditions in while loops):

      • [ ... ] is in fact another way to write test ... – this is simply a command which may return 0 (true) or 1 (false) depending on what parameters you are giving. (This is a buildin of the shell.)

      • [[ ... ]] is a conditional expression command. It supports some of the same arguments that [ supports, and some more, like parentheses, <, >, and ( … ) for condition nesting, and handles expansions inside a bit different, since it is a special syntactic construct instead of a buildin command. The whole thing is a command that returns 0 (true) or 1 (false).

      • (( ... )) is a wrapper for an arithmetic expression (or several of them) as a command. The result is 0 (true) when the result of the (last) expression is non-0, and 1 (false) when the result of the (last) expression is 0. (You could instead write let ....)

        In arithmetic expressions the shell variables can be used without $, and everything is interpreted as integers (of a fixed width, but I didn’t find out which). You can use these operators:

        • unary: ++, --, +, -, !, ~ (like in C)
        • binary arithmetic: ** (exponentation), *, /, %, +, -
        • bit-shift: <<, >>
        • comparison: <=, >=, <, >, ==, != (I think these return either 1 (true) or 0 (false))
        • bitwise: &, ^, |
        • logical: &&, ||, and of course the ternary operator: ... ? ... : ...
        • assignment: =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
        • comma: , (simply connects two expressions, returning the value of the last one)

        I think in general this works a lot like C integer expressions. (These are roughly in order of precedence, but sometimes there are divisions inside of each group. Look in info '(bash)Shell Arithmetic' for details.)

      • true is a buildin which does nothing and always returns 0.

      • false is a buildin which does nothing and always returns 1.

      • you can also call your external C (or anything else) programs as commands, their return values are interpreted the same way.

    • Lists:

      • ( ... ) is a list of commands, which is executed in a subshell. Variable assignments in this subshell are not propagated back.
      • { ...; } is a list of commands, which does not create a subshell.
      • ; is a simple separator of commands in a list, but can also be replaced by a new-line.
      • && is a separator of commands executing the second one only when the first returned 0.
      • || is a separator of commands executing the second one only when the first returned not-0.
      • & is a separator of commands, executing both commands in parallel (the first one in background).

    So, you can write your if-command with some braces and parentheses, but you still need your then and fi:

    if (( i > 0 ))
    then {
       echo "i > 0"
    }
    else {
       echo "i <= 0"
    }
    fi
    

    The braces here are simply superfluous, since the commands between then, else and fi are (each) one list anyway. (Also note the need of new-lines or ; after and before the closing braces here.)

    But as the others said, better use another language if you want to use a C-like syntax.

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

Sidebar

Related Questions

While HTML Scraping is pretty well-documented from what I can see, and I understand
As I understand it I can use reverse P/Invoke to call C# from C++.
Can someone please explain how to understand a logcat from an android force close.
Using a delegate I can call any function asynchronously. From the documentation I understand
while ($row = mysql_fetch_object($result)) { $data[] = $row; echo <div id='captionbox' style='width: 110px;float:left;color:#FFF;text-align:center;'>; echo
I can't currently understand why my update method in my repository for the style
I am using DBus in a project. I understand from DBus specification that for
From what I understand, the parent attribute of a db.Model (typically defined/passed in the
From what I understand, due to the same origin policy enforcement in current browsers,
From what I understand, in TDD you have to write a failing test first,

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.