{"id":13214,"date":"2024-03-15T06:04:40","date_gmt":"2024-03-15T06:04:40","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/"},"modified":"2025-08-05T13:53:12","modified_gmt":"2025-08-05T13:53:12","slug":"a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/","title":{"rendered":"Linux Shell Arithmetic Operations"},"content":{"rendered":"<p>In the Linux Shell, basic integer arithmetic operations can be performed using the built-in command expr, while the bc command can be used for floating point calculations.<\/p>\n<p>Below is an example code for integer operations.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta prompt_\">#<\/span><span class=\"language-bash\">!\/bin\/bash<\/span>\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u6574\u6570\u56db\u5219\u8fd0\u7b97<\/span>\r\nnum1=10\r\nnum2=5\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u52a0\u6cd5<\/span>\r\nresult=$(expr $num1 + $num2)\r\necho \"\u52a0\u6cd5\u7ed3\u679c\uff1a$result\"\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u51cf\u6cd5<\/span>\r\nresult=$(expr $num1 - $num2)\r\necho \"\u51cf\u6cd5\u7ed3\u679c\uff1a$result\"\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u4e58\u6cd5<\/span>\r\nresult=$(expr $num1 \\* $num2)\r\necho \"\u4e58\u6cd5\u7ed3\u679c\uff1a$result\"\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u9664\u6cd5<\/span>\r\nresult=$(expr $num1 \/ $num2)\r\necho \"\u9664\u6cd5\u7ed3\u679c\uff1a$result\"\r\n<\/code><\/pre>\n<p>An example code for arithmetic operations with floating point numbers is shown below:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta prompt_\">#<\/span><span class=\"language-bash\">!\/bin\/bash<\/span>\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u6d6e\u70b9\u6570\u56db\u5219\u8fd0\u7b97<\/span>\r\nnum1=10.5\r\nnum2=2.5\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u52a0\u6cd5<\/span>\r\nresult=$(echo \"$num1 + $num2\" | bc)\r\necho \"\u52a0\u6cd5\u7ed3\u679c\uff1a$result\"\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u51cf\u6cd5<\/span>\r\nresult=$(echo \"$num1 - $num2\" | bc)\r\necho \"\u51cf\u6cd5\u7ed3\u679c\uff1a$result\"\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u4e58\u6cd5<\/span>\r\nresult=$(echo \"$num1 * $num2\" | bc)\r\necho \"\u4e58\u6cd5\u7ed3\u679c\uff1a$result\"\r\n<span class=\"hljs-meta prompt_\">\r\n# <\/span><span class=\"language-bash\">\u9664\u6cd5<\/span>\r\nresult=$(echo \"scale=2; $num1 \/ $num2\" | bc)\r\necho \"\u9664\u6cd5\u7ed3\u679c\uff1a$result\"\r\n<\/code><\/pre>\n<p>In the provided code, the expr command is used for integer operations while the bc command is used for floating-point calculations. The scale option in the bc command is used to set the precision of floating-point calculations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the Linux Shell, basic integer arithmetic operations can be performed using the built-in command expr, while the bc command can be used for floating point calculations. Below is an example code for integer operations. #!\/bin\/bash # \u6574\u6570\u56db\u5219\u8fd0\u7b97 num1=10 num2=5 # \u52a0\u6cd5 result=$(expr $num1 + $num2) echo &#8220;\u52a0\u6cd5\u7ed3\u679c\uff1a$result&#8221; # \u51cf\u6cd5 result=$(expr $num1 &#8211; $num2) echo [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[8759,4576,16854,17532,17531],"class_list":["post-13214","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-arithmetic-operations","tag-bash-scripting","tag-bc-command","tag-expr-command","tag-linux-shell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux Shell Arithmetic Operations - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to perform integer and floating point arithmetic in Linux shell using expr and bc commands with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux Shell Arithmetic Operations\" \/>\n<meta property=\"og:description\" content=\"Learn how to perform integer and floating point arithmetic in Linux shell using expr and bc commands with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T06:04:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T13:53:12+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin Taylor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Taylor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/\"},\"author\":{\"name\":\"Benjamin Taylor\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\"},\"headline\":\"Linux Shell Arithmetic Operations\",\"datePublished\":\"2024-03-15T06:04:40+00:00\",\"dateModified\":\"2025-08-05T13:53:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/\"},\"wordCount\":89,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"arithmetic operations\",\"bash scripting\",\"bc command\",\"expr command\",\"linux shell\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/\",\"name\":\"Linux Shell Arithmetic Operations - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T06:04:40+00:00\",\"dateModified\":\"2025-08-05T13:53:12+00:00\",\"description\":\"Learn how to perform integer and floating point arithmetic in Linux shell using expr and bc commands with examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux Shell Arithmetic Operations\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\",\"name\":\"Benjamin Taylor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"caption\":\"Benjamin Taylor\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Linux Shell Arithmetic Operations - Blog - Silicon Cloud","description":"Learn how to perform integer and floating point arithmetic in Linux shell using expr and bc commands with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/","og_locale":"en_US","og_type":"article","og_title":"Linux Shell Arithmetic Operations","og_description":"Learn how to perform integer and floating point arithmetic in Linux shell using expr and bc commands with examples.","og_url":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T06:04:40+00:00","article_modified_time":"2025-08-05T13:53:12+00:00","author":"Benjamin Taylor","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Benjamin Taylor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/"},"author":{"name":"Benjamin Taylor","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9"},"headline":"Linux Shell Arithmetic Operations","datePublished":"2024-03-15T06:04:40+00:00","dateModified":"2025-08-05T13:53:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/"},"wordCount":89,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["arithmetic operations","bash scripting","bc command","expr command","linux shell"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/","url":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/","name":"Linux Shell Arithmetic Operations - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T06:04:40+00:00","dateModified":"2025-08-05T13:53:12+00:00","description":"Learn how to perform integer and floating point arithmetic in Linux shell using expr and bc commands with examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/a-simple-method-to-perform-basic-arithmetic-operations-integer-and-floating-point-in-the-linux-shell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Linux Shell Arithmetic Operations"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9","name":"Benjamin Taylor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","caption":"Benjamin Taylor"},"url":"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=13214"}],"version-history":[{"count":3,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13214\/revisions"}],"predecessor-version":[{"id":157121,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13214\/revisions\/157121"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=13214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=13214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=13214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}