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:
look command in Linux with Examples
Next article icon

let command in Linux with Examples

Last Updated : 31 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The `let` command in Linux is a powerful tool used for evaluating arithmetic expressions on shell variables. It supports various operators and functionalities to perform calculations and manipulate values.

Syntax of `let` command in Linux 

The basic syntax of the `let` is as follows.

let [expression]

Here, [expression] represents the arithmetic expression to be evaluated.

Options available in `let` command

Basic arithmetic operators

The addition(+), subtraction(-), multiplication(*), division(/) and modulus(%) operators can be used in the expression with the let command. 

Example:

let "myvar =2" "myvar1=1" "myvar2=myvar1+myvar"; echo $myvar2
let "myvar =2" "myvar1=1" "myvar2=myvar1-myvar"; echo $myvar2
let "myvar =2" "myvar1=1" "myvar2=myvar1*myvar"; echo $myvar2
let "myvar =2" "myvar1=1" "myvar2=myvar1/myvar"; echo $myvar2
let "myvar =2" "myvar1=1" "myvar2=myvar1%myvar"; echo $myvar2

Post-increment(var++) / post-decrement(var–) operator:

This operator is used to interpret the integer value then increase/decrease the integer variable by 1.

Example:

let "myvar=2" "myvar2=myvar++" ; echo $myvar $myvar2

The above example, myvar2 gets the value of myvar2 before the increment occurs.

Pre-increment(++var) / Pre-decrement(–var) operator:

This operator increases/decreases the integer value by 1 and then interprets the integer variable.

Example:

let "myvar=10" "myvar2=--myvar"; echo $myvar $myvar2

In the above example, the change in value occurs first then the value gets stored in myvar2.

Unary plus(+exp) / Unary minus(-exp):

This operator is used to multiply a given expression by 1 or -1. 

Example: 

In the above example, the value of myvar changes from positive to negative with unary minus operator.

Bit-wise negation (~) :

This operator is used to negate every bit of the integer value i.e., turns 0 to 1 and 1 to 0.
Example:

let "myvar=0" "myvar= ~myvar"; echo $myvar

In the above example, the value myvar is ‘0000…00’ in binary while the negation is ‘1111…11’ which is the 2’s complement value of -1.

Exponent (**) operator :

This operator is used to raise one quantity to the power of another.
Example:

let "myvar= 5 ** 2" ; echo $myvar

Bitwise shift left / Bitwise shift right :

This operator is used to shift the order of the bits either to the left or right.

Example:

let "myvar = 5 << 2"; echo $myvar

Bitwise AND operator:

This operator does a bitwise comparison between two bits and returns 1 if both are 1 else returns 0.

Example:

let "myvar=5" "myvar2=4" "myvar3 = myvar & myvar2" ; echo $myvar3

Bitwise OR operator:

This operator makes a bitwise comparison between two bits and returns 1 if atleast one the bits is 1, else returns 0.

Example:

let "myvar=7" "myvar2=4" "myvar3= myvar | myvar2" ; echo $myvar3

Bitwise XOR operator:

This operator does a bitwise comparison between two bits and returns 0 if they are alike, else returns 1.

Example:

let "myvar=7" "myvar2=4" "myvar3= myvar ^ myvar2" ; echo $myvar3

Conclusion

In this article we have discussed `let` command in Linux which is used in evaluation of arithmetic expressions on shell variables. By understanding the syntax and utilizing the various options available with the `let` command, we can leverage its power to handle complex arithmetic operations within the Linux command line environment.


Next Article
look command in Linux with Examples

B

basilmohamed
Improve
Article Tags :
  • Linux-Unix
  • linux-command
  • Linux-Shell-Commands

Similar Reads

  • ip Command in Linux with Examples
    The ip command in Linux is a powerful utility for network configuration and management. It allows users to interact with various networking components such as network interfaces, routing tables, addresses, and more. Here, we will look into the 'ip' command, covering each aspect with examples, code,
    7 min read
  • ipcrm command in Linux with examples
    ipcrm command in Linux is used to remove some IPC(Inter-Process Communication) resources. It eliminates the IPC objects and their associated data structure form the system. One must be a creator or superuser or the owner of the object in order to remove these objects. There are three types of System
    2 min read
  • 'IPCS' command in Linux with examples
    ipcs shows information on the inter-process communication facilities for which the calling process has read access. By default, it shows information about all three resources: shared memory segments, message queues, and semaphore arrays. Without options, the information shall be written in short for
    3 min read
  • iptables command in Linux with Examples
    The iptables command in Linux is a powerful tool that is used for managing the firewall rules and network traffic. It facilitates allowing the administrators to configure rules that help how packets are filtered, translated, or forwarded. On using this iptables, you can set up security policies to c
    8 min read
  • iptables-save command in Linux with examples
    The information transfer from a remote computer to your local computer and the vice-versa are viewed as the transfer of data packets by the firewall. The firewall has the control of data packets that are both incoming and outgoing.iptables is a utility to create a rule-based firewall that is pre-ins
    3 min read
  • iwconfig command in Linux with Examples
    Wireless networking plays a crucial role in modern computing, enabling devices to connect and communicate without the need for physical cables. In the Linux ecosystem, the iwconfig command stands as a powerful tool for configuring and managing wireless network interfaces. This article will delve int
    5 min read
  • join Command in Linux
    The 'join' command in UNIX is a powerful command-line utility that allows you to merge lines from two files based on a common field, effectively combining related data into a more meaningful format. For instance, if you have one file with names and another with IDs, the join command can combine thes
    7 min read
  • journalctl Command in Linux with Examples
    The `journalctl` command is part of the systemd suite of utilities and is used to query and display log messages from the systemd journal. The systemd journal is a centralized logging system that collects and stores log data from various sources, including system services, kernel events, and user ap
    7 min read
  • How to Kill a Process in Linux | Kill Command
    kill command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually. kill command sends a signal to a process that terminates the process. If the user doesn't specify any signal that is to be sent along with the kill command, then a default TERM signal i
    6 min read
  • last command in Linux with Examples
    The 'last' command in Linux is a powerful utility used to display a list of all users who have logged in and out since the creation of the '/var/log/wtmp' file. It provides a chronological view of user sessions, allowing administrators to monitor user activity, troubleshoot issues, and manage system
    5 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