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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:31:21+00:00 2026-05-22T03:31:21+00:00

I just discovered this great site. I was reading bash tagged posts when the

  • 0

I just discovered this great site. I was reading bash tagged posts when the following question entered my mind:

This code:

var=$RANDOM

var1=$[ $var % 13 ]
echo "var1 = $var1"

var2=$( $var % 13 )
echo "var2 = $var2"

var3=$(( $var % 13 ))
echo "var3 = $var3"

var4=`expr $var % 13` # Note revised from original post by adding the expr
echo "var4 = $var4"

Produces this output:

var1 = 7
./question: line 7: 23225: command not found
var2 = 
var3 = 7
var4 = 7

So only the var2 statement does not work. My question is: is it only a matter of personal choice as to which of the other three should be used or are there other considerations that should be taken into account?

(I’ve edited this question extensively after seeing replies. Hope I’m doing this the right way.)

  • 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-22T03:31:22+00:00Added an answer on May 22, 2026 at 3:31 am

    The $[] and $(()) versions are essentially interchangeable, except that not all shells support $[], so don’t use it — use $(()) instead. Also, in both of these forms, variables used in the expression will be automatically expanded, so you don’t need to use $ inside them:

    var=$RANDOM
    echo $var             # prints 7482 (in my example run)
    echo $[ $var % 13 ]   # prints 7
    echo $[ var % 13 ]    # prints 7
    echo $(( $var % 13 )) # prints 7
    echo $(( var % 13 ))  # prints 7
    

    expr is actually a command which does expression evaluation — it has most of the same capabilities as the builtin expression evaluation, but without some of the niceties. For instance, you must use $ to expand variables, and must escape any operators that the shell would treat as special characters (e.g. <, >, |, etc):

    expr $var % 13        # prints 7
    echo `expr $var % 13` # prints 7, just by a less direct route
    

    $() is totally different — it doesn’t do arithmetic evaluation, it runs its contents as a command. In fact, it’s almost the same as backquotes (except that it is easier to read, and has much cleaner syntax, so I always use it instead of backquotes):

    echo $(expr $var % 13) # prints 7, same as with backquotes
    

    And just to make this more complicated, let me add some more options:

    (( var5 = var % 13 )) # This is an arithmetic statement (that happens to contain an assignment)
    echo $var5            # prints 7
    let "var6 = var % 13" # Another form for pretty much the same thing
    echo $var6            # prints 7
    
    declare -i var7  # declare var7 as an integer...
    var7="var % 13"  #  to force values assigned to it to be evaluated
    echo $var7       # prints 7
    

    So what should you use? I’d go with var3=$(( var % 13 )) for maximum compatibility and (IMHO) clean syntax.

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

Sidebar

Related Questions

I just discovered a bug where the code looked something like this: char *foo
I just discovered this component and started working with it. I understand that the
Possible Duplicate: Python or Ruby Interpreter on iOS I just discovered this apps pypad
I am working with zend just recently. I discovered this ViewScript decorator for form
I just discovered the what c# knowledge should I have? question and wondered about
i just discovered http://code.google.com/p/re2 , a promising library that uses a long-neglected way (
I just discovered selenium - a great tool! I plan to run/use selenium-ide generated
I just discovered there is a Ajax success handler, which is great because its
just doing my Homeworks and discovered this piece A[j]=A[j-1]; j--; is there a way
I just discovered a very strange behavior with Type.GetInterface and nested Types. The following

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.