Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
fc Command in Linux with Examples
Next article icon

expr command in Linux with examples

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The expr command in Unix is a versatile tool used for evaluating expressions in the shell environment. It performs a wide range of functions, including arithmetic calculations, string operations, and comparisons based on regular expressions.

What is the ‘expr’ Command?

expr stands for “expression” and allows for the evaluation of values and returns the result to standard output. It is particularly useful in scripts for handling both numerical and string data efficiently. In short, it helps in:

  • Basic operations like addition, subtraction, multiplication, division, and modulus on integers.
  • Evaluating regular expressions, string operations like substring, length of strings etc.

Syntax:

$expr expression 

Key Options:

  • ‘–version’: It is used to show the version information. Syntax:
    $expr --version 

    Example:

  • ‘–help’: It is used to show the help message and exit. Syntax:
    $expr --help 
  • Example:

‘expr’ Command Examples

Below are some examples to demonstrate the use of “expr” command:

1. Using expr for basic arithmetic operations :

Example 1: Addition

$expr 12 + 8  

Example 2: Multiplication

$expr 12 \* 2 

Output

Note: The multiplication operator * must be escaped when used in an arithmetic expression with ‘expr’.

2. Performing operations on variables inside a shell script

Example: Adding two numbers in a script

echo "Enter two numbers" read x  read y sum=`expr $x + $y` echo "Sum = $sum" 

Output:

Note: ‘expr’ is an external program used by Bourne shell. It uses ‘expr’ external program with the help of backtick. The backtick(`) is actually called command substitution.

3. Comparing two expressions

Example 1: Equality Check

x=10 y=20  # matching numbers with '=' res=`expr $x = $y` echo $res  # displays 1 when arg1 is less than arg2 res=`expr $x \< $y` echo $res  # display 1 when arg1 is not equal to arg2 res=`expr $x \!= $y` echo $res 

Output:

Example 2: Evaluating boolean expressions

# OR operation $expr length  "geekss"  "<"  5  "|"  19  -  6  ">"  10 

Output:

# AND operation $expr length  "geekss"  "<"  5  "&"  19  -  6  ">"  10 

Output:

4. For String operations

Example 1: Finding length of a string

x=geeks len=`expr length $x` echo $len 

Output:

Example 2: Finding substring of a string

x=geeks  sub=`expr substr $x 2 3`  #extract 3 characters starting from index 2  echo $sub 

Output:

Example 3: Matching number of characters in two strings

$ expr geeks : geek 

Output:

Conclusion

The ‘expr’ command is an indispensable tool for scripting in Unix-like systems, offering a broad array of functionalities from mathematical calculations to complex string manipulations. By mastering ‘expr’, users can enhance their scripts’ capabilities and perform sophisticated data processing tasks efficiently.


Next Article
fc Command in Linux with Examples

T

tanuja_tj
Improve
Article Tags :
  • Linux-Unix

Similar Reads

  • ed command in Linux with examples
    Linux 'ed' command is used to start the ed text editor, a very minimal fronted line-based text editor. The lines are very simple in relation to other text files, hence easy to work on, for creating as well as editing, display, and manipulation of files. Since it was the first editor that was include
    4 min read
  • egrep command in Linux with examples
    The 'egrep' command in Linux is a powerful pattern-searching utility that belongs to the family of grep functions. It functions similarly to 'grep -E' by treating patterns as extended regular expressions, allowing more complex pattern matching without the need to escape special characters. This comm
    3 min read
  • eject command in Linux with examples
    eject allows ejecting a removable media (typically a CD-ROM, floppy disk, tape, or JAZ or ZIP disk) using the software. You can also control some multi-disc CD-ROM changers, the auto-eject feature (supported by some devices), and close the disc tray of some CD-ROM drives. Syntaxeject [...OPTIONS]The
    4 min read
  • emacs command in Linux with examples
    Introduction to Emacs Editor in Linux/Unix Systems: The Emacs is referred to a family of editors, which means it has many versions or flavors or iterations. The most commonly used version of Emacs editor is GNU Emacs and was created by Richard Stallman. The main difference between text editors like
    5 min read
  • env command in Linux with Examples
    The 'env' command in Linux is a versatile tool used to manage environment variables. It can print all the current environment variables, set new ones, or run a command within a custom environment. Additionally, 'env' is commonly used in shell scripts to launch the correct interpreter, ensuring the s
    3 min read
  • eval command in Linux with Examples
    eval is a built-in Linux command that executes arguments as a shell command. It combines arguments into a single string, uses it as input to the shell, and executes the commands. Syntaxeval [arg ...]Here, arg can be one or more arguments that eval will process and combine into a single shell command
    2 min read
  • ex command in Linux with examples
    ex (stands for extended) is a text editor in Linux which is also termed as the line editor mode of the vi editor. This editor simply provided some editing commands which has greater mobility. With the help of this editor, a user can easily move between files. Also, he/she has a lot of ways to transf
    4 min read
  • exec command in Linux with examples
    The 'exec' command in Linux is a powerful shell built-in used to replace the current shell process with a new command process. Unlike typical commands that create a new process, 'exec' transforms the existing process, directly impacting the efficiency and behavior of scripts. What is the exec Comman
    3 min read
  • exit command in Linux with Examples
    The 'exit' command is a fundamental command in Linux used to exit the current shell session. This command is widely used in scripting and command-line operations, allowing users and scripts to terminate a session or a process in a controlled manner. The 'exit' command can take an optional parameter
    2 min read
  • expand Command in LINUX with examples
    Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences