{"id":79079,"date":"2026-11-17T01:54:37","date_gmt":"2026-11-17T01:54:37","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=79079"},"modified":"2026-11-17T01:54:37","modified_gmt":"2026-11-17T01:54:37","slug":"linux-intermediate-03-bash","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=79079","title":{"rendered":"Linux Intermediate 03 \u2014 Bash Variables, Quoting, and Input"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/bangla.sitestree.com\/?name=linux-course-index\">Linux course index<\/a> \u00b7 <a href=\"https:\/\/bangla.sitestree.com\/?cat=1991\">Linux Intermediate category<\/a> \u00b7 Lesson 3 of 9<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>AI disclosure<\/strong> \u2014 AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Learning goal<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write Bash variables, capture a command result, and accept input without losing spaces or misinterpreting nonnumeric input.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Concepts and setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use name=value with no spaces around the equals sign. Single quotes preserve literal text. Double quotes still allow expansions while keeping the result a single argument. read -r keeps backslashes and IFS= avoids trimming leading or trailing whitespace. Export a variable only when child processes need to see it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This adapts the site&#8217;s Korn shell date example and its two-number Bash arithmetic example. The two-digit validation in this lab keeps arithmetic bounded and a decimal prefix prevents accidental octal interpretation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Capture the date and preserve a value with spaces<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>day=$(date +%F)\nprintf 'Today is %s.\\n' &quot;$day&quot;\nname='weekly report'\nprintf '&lt;%s&gt;\\n' &quot;$name&quot;\nprintf '%s\\n' '$name'\nexport name\nbash -c 'printf &quot;child sees: %s\\n&quot; &quot;$name&quot;'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Today is 2026-11-16.\n&lt;weekly report&gt;\n$name\nchild sees: weekly report<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The date is illustrative. printf has a fixed format string and one quoted argument. The child process sees name because it was exported.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Build a decimal arithmetic script<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>lab=$(mktemp -d)\ncd &quot;$lab&quot; || exit\ncat &gt; calc.sh &lt;&lt;'EOF'\n#!\/usr\/bin\/env bash\nIFS= read -r a || exit 2\nIFS= read -r b || exit 2\nif ! [[ $a =~ ^[0-9]{1,2}$ &amp;&amp; $b =~ ^[0-9]{1,2}$ ]]; then\n  printf 'Enter two integers from 0 to 99\\n' &gt;&amp;2\n  exit 2\nfi\na=$((10#$a))\nb=$((10#$b))\nprintf 'sum=%d product=%d\\n' &quot;$((a+b))&quot; &quot;$((a*b))&quot;\nif (( b == 0 )); then\n  printf 'division skipped: zero divisor\\n'\nelse\n  printf 'integer quotient=%d\\n' &quot;$((a\/b))&quot;\nfi\nEOF\nprintf '20\\n05\\n' | bash calc.sh\nprintf '8\\n0\\n' | bash calc.sh<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Possible output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sum=25 product=100\ninteger quotient=4\nsum=8 product=0\ndivision skipped: zero divisor<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The quoted heredoc sends the script verbatim to cat. Use Bash to run it because [[ ]] and the arithmetic details here are Bash-specific.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Questions only<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Quick-check questions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li>Why do we use read -r?<\/li>\n\n\n<li>What does double quoting &quot;$name&quot; protect against?<\/li>\n\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Assessment questions<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li>Predict the output with inputs 9 and 2. Why not 4.5?<\/li>\n\n\n<li>What should the script return when fed an empty field?<\/li>\n\n\n<li>Add a line that prints a decimal remainder without dividing by zero.<\/li>\n\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Questions and answers<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Quick-check answers<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li><strong>Why do we use read -r?<\/strong><br><strong>Answer<\/strong> \u2014 It prevents read from treating backslashes as escapes.<\/li>\n\n\n<li><strong>What does double quoting &quot;$name&quot; protect against?<\/strong><br><strong>Answer<\/strong> \u2014 It keeps the expanded value as one argument and prevents pathname expansion on that value.<\/li>\n\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Assessment answer key<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n\n<li><strong>Predict the output with inputs 9 and 2. Why not 4.5?<\/strong><br><strong>Answer<\/strong> \u2014 sum=11 product=18 and integer quotient=4. Bash arithmetic here uses integer division.<\/li>\n\n\n<li><strong>What should the script return when fed an empty field?<\/strong><br><strong>Answer<\/strong> \u2014 2 for invalid input or a missing input line.<\/li>\n\n\n<li><strong>Add a line that prints a decimal remainder without dividing by zero.<\/strong><br><strong>Answer<\/strong> \u2014 Inside the nonzero-divisor branch, add printf &#8216;remainder=%d\\n&#8217; &quot;$((a%b))&quot;.<\/li>\n\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Source articles on this site<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=65742\">Topic 3: Example: Current Info : Variables #Linux\/Unix: Shell Programming \u2013 001<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=65685\">A Basic Shell Program #Linux\/Unix: Shell Programming \u2013 001<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=65691\">Take Two values from the user and do arithmetic operations #Linux\/Unix: Shell Programming \u2013 001<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=76382\">Linux Script: Read Data from User and Show Some Columns<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=9641\">\u0987\u0989\u09a8\u09bf\u0995\u09cd\u09b8\u0983 \u09b8\u09c7\u09b2 \u0995\u09cb\u099f\u09bf\u0982 \u09ae\u09c7\u0995\u09be\u09a8\u09bf\u099c\u09ae (Unix \u2013 Shell Quoting Mechanisms)<\/a><\/li>\n\n\n<li><a href=\"https:\/\/bangla.sitestree.com\/?p=78657\">20 mixed quiz questions on Quoting and Control Statements.<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Technical references<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li><a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/bash.html\">GNU Bash Reference Manual<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Continue the course<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Previous lesson 02 \u2014 Access Control Lists and Shared Directories. Next lesson 04 \u2014 Bash Conditions, Loops, Functions, and Debugging.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/bangla.sitestree.com\/?name=linux-course-index\">Linux course index<\/a> \u00b7 <a href=\"https:\/\/bangla.sitestree.com\/?cat=1991\">Linux Intermediate category<\/a> \u00b7 Lesson 3 of 9<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write Bash variables, capture a command result, and accept input without losing spaces or misinterpreting nonnumeric input.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1991],"tags":[],"class_list":["post-79079","post","type-post","status-publish","format-standard","hentry","category-linux-intermediate","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":79102,"url":"http:\/\/bangla.sitestree.com\/?p=79102","url_meta":{"origin":79079,"position":0},"title":"Linux Administration Course \u2014 Start Here and Lesson Index","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"A 33-lesson Linux course from shell fundamentals to server administration, performance, and a recovery capstone.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79080,"url":"http:\/\/bangla.sitestree.com\/?p=79080","url_meta":{"origin":79079,"position":1},"title":"Linux Intermediate 04 \u2014 Bash Conditions, Loops, Functions, and Debugging","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Use a case statement, a function, and a loop to generate a multiplication chart and test invalid input.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79072,"url":"http:\/\/bangla.sitestree.com\/?p=79072","url_meta":{"origin":79079,"position":2},"title":"Linux Basics 04 \u2014 Pipes, Redirection, and Exit Status","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Route input, normal output, and errors, then detect a command failure inside a pipeline.","rel":"","context":"In &quot;Linux Basics&quot;","block_context":{"text":"Linux Basics","link":"http:\/\/bangla.sitestree.com\/?cat=1990"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79081,"url":"http:\/\/bangla.sitestree.com\/?p=79081","url_meta":{"origin":79079,"position":3},"title":"Linux Intermediate 05 \u2014 Package Managers and Repositories","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Inspect installed packages and repositories before taking action, and tell a package database from a dependency resolver.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":79078,"url":"http:\/\/bangla.sitestree.com\/?p=79078","url_meta":{"origin":79079,"position":4},"title":"Linux Intermediate 02 \u2014 Access Control Lists and Shared Directories","author":"Sayed","date":"November 17, 2026","format":false,"excerpt":"Grant and remove a narrowly scoped named-user ACL and distinguish a grant from effective access.","rel":"","context":"In &quot;Linux Intermediate&quot;","block_context":{"text":"Linux Intermediate","link":"http:\/\/bangla.sitestree.com\/?cat=1991"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65687,"url":"http:\/\/bangla.sitestree.com\/?p=65687","url_meta":{"origin":79079,"position":5},"title":"Examples of using variables in a Shell Program #Linux\/Unix: Shell Programming &#8211; 001","author":"Author-Check- Article-or-Video","date":"July 11, 2021","format":false,"excerpt":"#!\/bin\/sh #assign value to a variable and print to the screen NAME=\"Justetc\" echo $NAME #Ask the user to enter a Name echo ################################## echo \"enter name\" #read command is used to take input from the user read X #print the value two times to the screen echo $X echo $X\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/79079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79079"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/79079\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79079"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}