← Back to Bash Course | Chapter 4: Loops | Lesson 1 of 14

expr कमांड

expr एक पुराने pocket calculator जैसा है जिसे हर number और sign को spaces से अलग रखने की ज़रूरत होती है इससे पहले कि यह आपको answer दे।

In this page:

  1. expr
Syntax
bash
result=$(expr operand1 operator operand2)

expr

expr एक पुराना external command है जो एक simple arithmetic या string expression evaluate करता है और result print करता है। इसे हर operator और operand के आसपास spaces चाहिए, $(( )) के विपरीत।

ज़्यादातर modern scripts $(( )) या let prefer करते हैं, लेकिन expr अब भी पुराने scripts में दिखता है।

Warning: expr 1+1 (बिना spaces) 2 के बजाय literally 1+1 print करता है — operators के आसपास spaces mandatory हैं।

उदाहरण: expr

bash
#!/bin/bash
result=$(expr 5 + 3)
echo "5 + 3 = $result"

result=$(expr 10 \* 2)
echo "10 * 2 = $result"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Operators के आसपास spaces भूल जाना, जैसे expr 1+2, जो 1+2 को text के रूप में print करता है।
  2. * को unescaped उपयोग करना, जैसे expr 2 * 3, जिसे shell एक file wildcard के रूप में expand करता है; \* लिखें।
  3. यह उम्मीद करना कि expr decimals करेगा, जब यह सिर्फ integers के साथ काम करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.