{"id":14629,"date":"2023-07-27T10:34:35","date_gmt":"2023-07-27T10:34:35","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=14629"},"modified":"2023-12-31T07:13:06","modified_gmt":"2023-12-31T07:13:06","slug":"bash-variable","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/","title":{"rendered":"Bash Variables"},"content":{"rendered":"<p style=\"text-align: justify;\"><strong>Bash variable<\/strong> stores essential data such as strings, numbers, and arrays of a Bash script. Bash programmer uses variables to manipulate data and do necessary arithmetic operations. In this article, I will explore various aspects of the Bash variable. Let\u2019s start and unleash the full potential of the Bash variable.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/bash-variables-scripts.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files of Bash Variables<\/span><\/a><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#What_is_Bash_Variable\" >What is Bash Variable?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Applications_of_Variables_in_Bash_Scripts\" >Applications of Variables in Bash Scripts<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Fundamental_Characteristics_of_Bash_Variables\" >Fundamental Characteristics of Bash Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Types_of_Variables_in_Linux\" >Types of Variables in Linux<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Some_Special_Variables_in_Linux\" >Some Special Variables in Linux<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Variable_Scope_in_Bash\" >Variable Scope in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Input_and_Export_of_Bash_Variables\" >Input and Export of Bash Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#7_Practical_Examples_of_Working_With_Bash_Variables\" >7 Practical Examples of Working With Bash Variables<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#1_Declaration_and_Assignment_of_Variables_in_Bash_Scripts\" >1. Declaration and Assignment of Variables in Bash Scripts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#2_Performing_Arithmetic_Operations_of_Variables_in_Bash_Scripting\" >2. Performing Arithmetic Operations of Variables in Bash Scripting<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#3_Use_of_Local_and_Global_Variables_in_Bash_Script\" >3. Use of Local and Global Variables in Bash Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#4_Using_Array_Variables_in_Bash_Script\" >4. Using Array Variables in Bash Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#5_Command_Substitution_With_Bash_Variables\" >5. Command Substitution With Bash Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#6_Working_With_Environment_Variables\" >6. Working With Environment Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#7_Quoting_String_in_a_Bash_Variable\" >7. Quoting String in a Bash Variable<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#What_is_the_use_of_a_variable_in_bash\" >What is the use of a variable in bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#What_are_the_rules_for_naming_variables_in_bash\" >What are the rules for naming variables in bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Are_bash_variables_all_stored_as_strings\" >Are bash variables all stored as strings?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#Where_is_the_shell_variable_set\" >Where is the shell variable set?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"What_is_Bash_Variable\"><\/span>What is Bash Variable?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A Bash\u00a0variable in Linux is a symbolic name or identifier that stores a value or text string. It is a way to store and manipulate data within a Bash script or the Bash environment. Variables in Bash scripts are typically used to hold temporary or user-defined data, making it easier to reference and manipulate values as needed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Applications_of_Variables_in_Bash_Scripts\"><\/span>Applications of Variables in Bash Scripts<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The variable can be used in the following situations:<\/p>\n<ol>\n<li>You can use Bash variables to <strong>store data<\/strong>.<\/li>\n<li><strong>Arithmetic operations<\/strong> can be done using the Bash variable.<\/li>\n<li>Taking valuable <strong>input from users<\/strong> and preserving it.<\/li>\n<li>Bash variable can be used to <strong>create dynamic output<\/strong>.<\/li>\n<li>The bash variable lets you <strong>access the environment variables<\/strong>.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Fundamental_Characteristics_of_Bash_Variables\"><\/span>Fundamental Characteristics of Bash Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Bash variables maintain some unique characteristics. These are discussed below:<\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>Declaring a Variable:<\/strong> Programmers do not need to declare Variables in Bash explicitly. They can assign a value to a variable directly, and Bash will automatically create it.<\/li>\n<li style=\"text-align: justify;\"><strong>Naming Convention of a Variable:<\/strong> Bash variables are<strong> case-sensitive<\/strong>. Var and var are treated as two different variables. You can use underscore(_) between the words of a variable name.<\/li>\n<li style=\"text-align: justify;\"><strong>Value Assigning in a Variable:<\/strong> Variables are assigned values using the \u2018=\u2019 operator. No spaces should be present around the \u2018=\u2019 sign. To assign value in variable_name variable, you have to execute the command: <code class=\"\" data-line=\"\">variable_name=value<\/code><\/li>\n<\/ul>\n<ul>\n<li style=\"text-align: justify;\"><strong>Accessing Values of Variable:<\/strong> To access the value stored in a variable, you can use the variable name preceded by a dollar sign(\u2018$\u2019). To print the value of variable_name, you have to execute the command: <code class=\"\" data-line=\"\">echo $variable_name<\/code><\/li>\n<li style=\"text-align: justify;\"><strong>Types of Variables: <\/strong>Bash does not have explicit variable types. All variables are treated as strings by default. However, you can perform numeric operations on variables containing numeric values.<\/li>\n<li style=\"text-align: justify;\"><strong>Usage of Variables:<\/strong> Variables can be used in various ways, such as <strong>arithmetic calculations, string manipulation, command substitution<\/strong>, and passing values between scripts or functions.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Types_of_Variables_in_Linux\"><\/span>Types of Variables in Linux<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are two types of variables. These are <strong>system variables<\/strong> and <strong>user-defined variables<\/strong>.<\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>System variables<\/strong> are variables defined by the system. These variables have been existing since the installation of the operating system. Some of the system variables with their function are given below:<\/li>\n<\/ul>\n<div class=\"su-table su-table-responsive su-table-alternate su-table-fixed\">\n<table width=\"624\">\n<thead>\n<tr>\n<th width=\"208\"><strong>Variable<\/strong><\/th>\n<th width=\"208\"><strong>Example<\/strong><\/th>\n<th width=\"208\"><strong>Function<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>SHELL<\/strong><\/td>\n<td style=\"width: 208px;text-align: center;vertical-align: middle\" width=\"208\">\/bin\/bash<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Defines the name of the shell used.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>PWD<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">\/root<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Shows the current Working directory.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>LOGNAME<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">root<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Defines the message of the dayfor the system.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>MOTD_SHOWN<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">pam<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Defines the message of the day for the system.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>HOME<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">root<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Home directory of the user.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>LS_COLORS<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">rs=0:di=01;34:ln=01;36:mh=00:pi=40;<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Used to set the colors the filenames will be displayed for the user.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>LESSCLOSE<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">\/usr\/bin\/lesspipe %s %s<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Used to invoke input postprocessor.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>USER<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">root<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Name of the current user.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>SHLVL<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">1<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Displays the number of shell levels the current shell is running on top of.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>SSH_CLIENT<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">185.185.185.185 54321 12<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">SSH client information [user IP] [user port] [Linux machine port].<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>PATH<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">\/usr\/local\/sbin<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Defines the directories to be searched for bash to find a command.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\"><strong>SSH_TTY<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">\/dev\/pts\/0_=\/usr\/bin\/printenv<\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"208\">Displays the path to the device associated with the current shell or command.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<ul>\n<li style=\"text-align: justify;\"><strong>User-defined variables<\/strong> are defined by the users to accomplish the task of the script. User-defined variables are whatever you want as variables in your script.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Some_Special_Variables_in_Linux\"><\/span>Some Special Variables in Linux<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">There are some special variables that stand for some special task. A list of special variables with their operation is given below:<\/p>\n<div class=\"su-table su-table-responsive su-table-alternate su-table-fixed\">\n<table width=\"624\">\n<thead>\n<tr>\n<th style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>Variables<\/strong><\/th>\n<th style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>Description<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>$#<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">Number of parameters passed to the script.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>$@<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">All the command line parameters are passed to the script.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>$?<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">The exit status of the last process to run.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>$$<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">The Process ID (PID) of the current script.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>$USER<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">The username of the user executing the script.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>$HOSTNAME<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">The hostname of the computer running the script.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>$SECONDS<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">The number of seconds the script has been running for.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>#RANDOM<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">Returns a random number.<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\"><strong>$LINENO<\/strong><\/td>\n<td style=\"text-align: center;vertical-align: middle\" width=\"312\">Returns the current line number of the script.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Variable_Scope_in_Bash\"><\/span>Variable Scope in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The scope of a variable in a program or script is a region where the variables have their existence. There are two types of variable scope. These are listed below:<\/p>\n<ul>\n<li><strong>Local Variable:<\/strong> If a variable is declared inside a function then it is generally a <strong>local variable<\/strong>.<\/li>\n<li><strong>Global Variable:<\/strong> If it is declared outside then it is a <strong>global variable.<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">But in the case of a bash script, whether it is written inside a function or outside a function by default is a global variable. To make a variable local, use the \u2018local\u2019 keyword.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Input_and_Export_of_Bash_Variables\"><\/span>Input and Export of Bash Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The simplest way to take any variable input from the user is to use the <code class=\"\" data-line=\"\">read<\/code> command. Follow the below syntax to take variable input from the user:<\/p>\n<p><code class=\"\" data-line=\"\">read -p\u00a0 variable_name<\/code><\/p>\n<p style=\"text-align: justify;\">The simplest way to export any variable and print it on the terminal is to use the <a href=\"https:\/\/linuxsimply.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a>. Follow the following syntax to do this:<\/p>\n<p><code class=\"\" data-line=\"\">echo $variable_name<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"7_Practical_Examples_of_Working_With_Bash_Variables\"><\/span>7 Practical Examples of Working With Bash Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Here I have listed <strong>7<\/strong> examples related to the Bash variable. I believe you will be able to improve your basic knowledge about bash variables by going through these.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Declaration_and_Assignment_of_Variables_in_Bash_Scripts\"><\/span>1. Declaration and Assignment of Variables in Bash Scripts<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Bash variables<\/strong> must be defined with proper name format. Then the programmer might assign a value to that variable or keep the variable empty. Here I will show you a script where I will declare a variable by assigning a value to it. Then print the value of a variable on the terminal. To achieve so, follow the bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#setting a variable\ndeclare a=10\n\n#printing the variable\necho $a<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <strong>#!\/bin\/bash<\/strong> interprets that it is a Bash script. Then, the value of the <strong>variable a<\/strong> is assigned. Afterward, the <code class=\"\" data-line=\"\">echo<\/code> command printed the value of \u201ca\u201d on the terminal.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-21060 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/1-variable-print.png\" alt=\"The Bash script has printed the value of the variable on the terminal that I have declared.\" width=\"700\" height=\"175\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/1-variable-print.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/1-variable-print-300x75.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The above image shows that the Bash script has printed the value of the variable on the terminal that I have declared.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Performing_Arithmetic_Operations_of_Variables_in_Bash_Scripting\"><\/span>2. Performing Arithmetic Operations of Variables in Bash Scripting<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can easily use variables in the Bash script. For using variables, you must mention the <strong>variable&#8217;s name <\/strong>correctly. The programmer can easily manipulate the value of a variable by an arithmetic operation.<\/p>\n<p style=\"text-align: justify;\">Here I will show you a script where the variable is taken from the user as input and then some arithmetic operations will be done on these variables. To know how to perform arithmetic operations of bash variables, follow the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#taking number as input from the user\nread -p &quot;Enter First Number: &quot; a\nread -p &quot;Enter Second Number: &quot; b\n\n#calculation summation\nvar=$((a*b))\n\n#printing the summation value\necho &quot;Multiplication of The Two Number:$var&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>At first, the Bash script has taken <strong>numbers a <\/strong>and<strong> b<\/strong> as input. Then I did the arithmetic multiplication. And finally, print the multiplication on the terminal.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter wp-image-21061 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/2-multiplication-print.png\" alt=\"The Bash script has taken the value of two numbers as input, calculated the multiplication, and printed the result on the terminal.\" width=\"825\" height=\"260\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/2-multiplication-print.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/2-multiplication-print-300x95.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/2-multiplication-print-768x242.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The above image illustrates that the Bash script has taken the value of two numbers as input, calculated the multiplication, and printed the result on the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Use_of_Local_and_Global_Variables_in_Bash_Script\"><\/span>3. Use of Local and Global Variables in Bash Script<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Programmers use a variable<strong> as local or global<\/strong> whatever they want. In this example, I will show you how to work with local and global variables. To use local and global variables in bash, follow the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n#setting value to number variable\nnumber=10\n#defining addition function\nfunction addition()\n{\n#defining number variable as local variable\nlocal number=1\n#defining m variable as local variable\nlocal m=10\n#adding number and m variable\n(( number=number+m ))\n#printing the value of number variable which is local\necho $number\n}\n#calling addition function\naddition\n#printing the value of number variable which is global\necho $number<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>At first <strong>number=10<\/strong> sets a <strong>global variable<\/strong> assigning a value 10 to it. Then the addition function is defined and the \u201cnumber\u201d variable is set as a <strong>local variable<\/strong> assigning a value of 10 to it and the same goes for the m variable. After that value of the <strong>number variable<\/strong> is added to the value of the <strong>m variable<\/strong> and the result is kept to the number variable. Afterward, the value of the number variable is printed on the terminal. Finally, after the end of the addition function, the value of the number variable is printed again. Where the printed value is the value of the global<strong> number variable<\/strong>.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-21062 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/3-local-and-global.png\" alt=\"The Bash script has worked with local and global variables.\" width=\"700\" height=\"202\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/3-local-and-global.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/3-local-and-global-300x87.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The image shows that the Bash script has worked with local and global variables.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Using_Array_Variables_in_Bash_Script\"><\/span>4. Using Array Variables in Bash Script<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">As a Bash programmer, you might need to use an <strong>array <\/strong>as a variable in Bash to preserve<strong> string data<\/strong>. Here I have a script where I will show the value of all elements of that script along with a key. To learn more about array variables, follow the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nmy_array=(Cook Graeem Lee Wade Mathew Jumpa)\n#count the total number of elements in an array\ntotal=${#my_array[*]}\necho &quot;Total array elements are: $total&quot;\n#display value of each element of an array\necho &quot;Array Elements values:&quot;\nfor val in ${my_array[*]}\ndo\nprintf &quot;\u00a0\u00a0 %s\\n&quot; $val\ndone\n#display each array\u2019s element value with a key\necho &quot;Array Elements values with key:&quot;\nfor key in ${!my_array[*]}\ndo\nprintf &quot;%4d: %s\\n&quot; $key ${my_array[$key]}\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">my_array=(Cook Graeem Lee Wade Mathew Jumpa)<\/code> command sets the value of the <strong>my_array<\/strong> variable. After that the <code class=\"\" data-line=\"\">total=${#my_array[*]}<\/code>\u00a0command calculates the length of the my_array variable and prints a line mentioning the total element number on that array. Afterward the for loop prints the value of all values inside of the <strong>my_array<\/strong> variable.\u00a0 Then the next <strong>for loop <\/strong>finds the key for all elements and prints all of the elements\u2019 values with the key on the terminal.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21063 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/4-array-elements-and-key.png\" alt=\"The script printed the total number of the elements, all elements of that script along with a key for every element.\" width=\"700\" height=\"596\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/4-array-elements-and-key.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/4-array-elements-and-key-300x255.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The image illustrates that the script printed the total number of the elements, all elements of that script along with a key for every element.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Command_Substitution_With_Bash_Variables\"><\/span>5. Command Substitution With Bash Variables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Here I have developed a Bash script that will <strong>substitute a command<\/strong>. So, to learn how to substitute commands with bash variables, check the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#command assignment to a variable\nvariable=$( ls \/usr | wc -l )\n\n#printing value on the terminal\necho Total $variable entries on usr directory<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">variable=$( ls \/usr | wc -l )<\/code> command has assigned a command to variable. Then the <code class=\"\" data-line=\"\">echo Total $variable entries on usr directory<\/code> command has printed a text on the terminal.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21064 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/5-command-substitution.png\" alt=\"A variable has substituted a command and printed the output of the command on the terminal.\" width=\"700\" height=\"169\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/5-command-substitution.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/5-command-substitution-300x72.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The image shows that a variable has substituted a command and printed the output of the command on the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Working_With_Environment_Variables\"><\/span>6. Working With Environment Variables<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Here, I will develop a Bash script that will print the <strong>SHELL, PWD, HOME and USER<\/strong> variable values which are <strong>environment variables<\/strong>. To know how to work with environment variables, check the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#priting SHELL, PWD, HOME, and USER variable value\necho &quot;This user is using $SHELL shell&quot;\necho &quot;Their working directory is: $PWD&quot;\necho &quot;While their home directory is: $HOME&quot;\necho &quot;And the user name is: $USER&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">echo &quot;This user is using $SHELL shell&quot;<\/code> , <code class=\"\" data-line=\"\">echo &quot;Their working directory is: $PWD&quot;<\/code> ,<code class=\"\" data-line=\"\">echo &quot;While their home directory is: $HOME&quot;<\/code> , and <code class=\"\" data-line=\"\">echo &quot;And the user name is: $USER&quot;<\/code> commands have printed the <strong>SHELL<\/strong>, <strong>PWD<\/strong>, <strong>HOME,<\/strong> and <strong>USER<\/strong> variable values respectively which are environment variables.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21065 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/6-environment-variable.png\" alt=\"The Bash script has printed the SHELL, PWD, HOME, and USER variable values on the terminal which are environment variables.\" width=\"700\" height=\"239\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/6-environment-variable.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/6-environment-variable-300x102.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The image shows that the Bash script has printed the <strong>SHELL<\/strong>, <strong>PWD<\/strong>, <strong>HOME<\/strong>, and <strong>USER<\/strong> variable values on the terminal which are environment variables.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"7_Quoting_String_in_a_Bash_Variable\"><\/span>7. Quoting String in a Bash Variable<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Here I will show you a Bash script that will print the string from the variable along with another string utilizing the <strong>quotes<\/strong>. To know how to quote string in a bash variable, check out the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#assigning string to quotes\nquotes=&quot;Command-line arguments&quot;\n\n#printing the value of quotes on the terminal\necho &quot;Quotes work like this: $quotes&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">quotes=&quot;Command-line arguments&quot;<\/code> command has assigned a string to the quotes variable. Then the <code class=\"\" data-line=\"\">echo &quot;Quotes work like this: $quotes&quot;<\/code> command has printed the string from the variable along with another string.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21066 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/7-quotes.png\" alt=\"The Bash script has printed the string from the variable along with another string utilizing the quotes.\" width=\"700\" height=\"175\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/7-quotes.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/7-quotes-300x75.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The image shows that the Bash script has printed the string from the variable along with another string utilizing the <strong>quotes<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In conclusion, I would like to say that the <strong>Bash variable<\/strong> is one of the most important parts of the Bash script. It makes the Bash script more effective for solving problems and automating tasks. After going through this article, I believe you will be productive enough to use the Bash variable on the script.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_use_of_a_variable_in_bash\"><\/span>What is the use of a variable in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Bash variable stores string or number temporarily. The bash variable is an integral part of Bash scripting. It enables users to write Bash scripts to solve complex problems.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_are_the_rules_for_naming_variables_in_bash\"><\/span>What are the rules for naming variables in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Variable names can contain <strong>alphanumeric characters and underscores<\/strong>. It can start with nothing but numbers.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Are_bash_variables_all_stored_as_strings\"><\/span>Are bash variables all stored as strings?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Bash variable stores <strong>all types of data in a variable as a string<\/strong>. By giving proper instructions, programmers can do arithmetic operations and comparisons on variables.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Where_is_the_shell_variable_set\"><\/span>Where is the shell variable set?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The shell contains the shell variables. They are often used to keep track of ephemeral data, like the current working directory.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"What is the use of a variable in bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Bash variable stores string or number temporarily. The bash variable is an integral part of Bash scripting. It enables users to write Bash scripts to solve complex problems.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What are the rules for naming variables in bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Variable names can contain alphanumeric characters and underscores. It can start with nothing but numbers.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Are bash variables all stored as strings?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Bash variable stores all types of data in a variable as a string. By giving proper instructions, programmers can do arithmetic operations and comparisons on variables.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Where is the shell variable set?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The shell contains the shell variables. They are often used to keep track of ephemeral data, like the current working directory.\"\n    }\n  }]\n}\n<\/script><br \/>\n<span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/variable-in-programming\/\" target=\"_blank\" rel=\"noopener\">What is Variable in Programming? [The Complete Guide]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/variables-in-bash-script\/\" target=\"_blank\" rel=\"noopener\">Introduction to Variables in Bash Scripting [An Ultimate Guide]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/declaration-and-assignment\/\" target=\"_blank\" rel=\"noopener\">Variable Declaration and Assignment<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/types\/\" target=\"_blank\" rel=\"noopener\">Types of Variables in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/\" target=\"_blank\" rel=\"noopener\">Variable Scopes in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/usage\/\" target=\"_blank\" rel=\"noopener\">Using Variables in Bash Scripting<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to\u00a0<a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;14629&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;5&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4.8&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;4.8\\\/5 - (5 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Bash Variables&quot;,&quot;width&quot;:&quot;136.7&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 136.7px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            4.8\/5 - (5 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash variable stores essential data such as strings, numbers, and arrays of a Bash script. Bash programmer uses variables to &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Bash Variables\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#more-14629\" aria-label=\"Read more about Bash Variables\">Read more<\/a><\/p>\n","protected":false},"author":314894,"featured_media":14638,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[123],"class_list":["post-14629","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-bash-scripting-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bash Variables - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Bash variable makes Bash script more powerful to solve various kinds of real life complex problems. Knowing about bash variable is a plus.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bash Variables - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Bash variable makes Bash script more powerful to solve various kinds of real life complex problems. Knowing about bash variable is a plus.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/dasgupta.susmit\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-27T10:34:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-31T07:13:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/bash-variable.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Susmit Das Gupta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Susmit Das Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/\"},\"author\":{\"name\":\"Susmit Das Gupta\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/38abbef9a1053d9faec8935dc39f10fa\"},\"headline\":\"Bash Variables\",\"datePublished\":\"2023-07-27T10:34:35+00:00\",\"dateModified\":\"2023-12-31T07:13:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/\"},\"wordCount\":2091,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/bash-variable.png\",\"keywords\":[\"bash scripting tutorial\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/\",\"name\":\"Bash Variables - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/bash-variable.png\",\"datePublished\":\"2023-07-27T10:34:35+00:00\",\"dateModified\":\"2023-12-31T07:13:06+00:00\",\"description\":\"Bash variable makes Bash script more powerful to solve various kinds of real life complex problems. Knowing about bash variable is a plus.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/bash-variable.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/bash-variable.png\",\"width\":400,\"height\":400,\"caption\":\"Bash variable\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bash Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/38abbef9a1053d9faec8935dc39f10fa\",\"name\":\"Susmit Das Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"caption\":\"Susmit Das Gupta\"},\"description\":\"Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/dasgupta.susmit\",\"https:\\\/\\\/www.instagram.com\\\/susmit_dasgupta\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/susmitdasgupta\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/susmit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bash Variables - LinuxSimply","description":"Bash variable makes Bash script more powerful to solve various kinds of real life complex problems. Knowing about bash variable is a plus.","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:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/","og_locale":"en_US","og_type":"article","og_title":"Bash Variables - LinuxSimply","og_description":"Bash variable makes Bash script more powerful to solve various kinds of real life complex problems. Knowing about bash variable is a plus.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/dasgupta.susmit","article_published_time":"2023-07-27T10:34:35+00:00","article_modified_time":"2023-12-31T07:13:06+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/bash-variable.png","type":"image\/png"}],"author":"Susmit Das Gupta","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Susmit Das Gupta","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/"},"author":{"name":"Susmit Das Gupta","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/38abbef9a1053d9faec8935dc39f10fa"},"headline":"Bash Variables","datePublished":"2023-07-27T10:34:35+00:00","dateModified":"2023-12-31T07:13:06+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/"},"wordCount":2091,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/bash-variable.png","keywords":["bash scripting tutorial"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/","name":"Bash Variables - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/bash-variable.png","datePublished":"2023-07-27T10:34:35+00:00","dateModified":"2023-12-31T07:13:06+00:00","description":"Bash variable makes Bash script more powerful to solve various kinds of real life complex problems. Knowing about bash variable is a plus.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/bash-variable.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/07\/bash-variable.png","width":400,"height":400,"caption":"Bash variable"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"Bash Variables"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/38abbef9a1053d9faec8935dc39f10fa","name":"Susmit Das Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","caption":"Susmit Das Gupta"},"description":"Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/dasgupta.susmit","https:\/\/www.instagram.com\/susmit_dasgupta\/","https:\/\/www.linkedin.com\/in\/susmitdasgupta\/"],"url":"https:\/\/linuxsimply.com\/author\/susmit\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/14629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314894"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=14629"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/14629\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/14638"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=14629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=14629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=14629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}