{"id":65790,"date":"2022-10-14T13:05:35","date_gmt":"2022-10-14T13:05:35","guid":{"rendered":"\/tutorials\/?p=65790"},"modified":"2025-11-28T12:37:36","modified_gmt":"2025-11-28T12:37:36","slug":"bash-variables","status":"publish","type":"post","link":"\/tutorials\/bash-variables","title":{"rendered":"What are Bash variables and how to use them effectively"},"content":{"rendered":"<p>Bash variables are named storage units that hold data values for reference and manipulation within the Linux command line environment. In shell scripting, they allow users to pass dynamic information, such as file paths, configuration settings, or command outputs, without writing them directly into their code.<\/p><p>Here&rsquo;s what you need to know about Bash variables:<\/p><ul class=\"wp-block-list\">\n<li><strong>Assigning Bash variables. <\/strong>Use a <strong>key=value <\/strong>format without spaces around the equal sign. Values containing whitespaces should be enclosed in quotation marks.<\/li>\n\n\n\n<li><strong>Different types of Bash variables<\/strong>. Set user-defined variables that are local to a particular script, or environment variables that are accessible to other processes within the same shell.<\/li>\n\n\n\n<li><strong>Bash variables&rsquo; scope<\/strong>. Determine where the variables can be accessed. By default, they are global within a script, but adding the local keyword restricts them to specific functions. <\/li>\n\n\n\n<li><strong>Special Bash variables. <\/strong>Use placeholders like<strong> $0<\/strong>, <strong>$$<\/strong>, and <strong>$?<\/strong> to automatically pass data, like script information, process IDs, and exit statuses, to your variables. <\/li>\n\n\n\n<li><strong>Exporting and unsetting variables<\/strong>. Pass variables to a script&rsquo;s child processes by exporting them. Meanwhile,  remove variables from the shell memory with<strong> unset<\/strong>.<\/li>\n\n\n\n<li><strong>Using quotes and command substitution. <\/strong>Use quotation marks and command substitution to control value interpretation. Single quotes preserve literal data, double quotes allow value expansion, while the <strong>$command<\/strong> captures command output as data.<\/li>\n\n\n\n<li><strong>Common Bash variable use cases. <\/strong>Combine Bash variables with conditional statements, loops, and arrays to pass data dynamically, which is helpful for automated tasks. <\/li>\n<\/ul><p>Before getting into these detailed usages, let&rsquo;s first learn the basics of assigning values to Bash variables.<\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-do-you-assign-values-to-variables-in-bash\"><strong>How do you assign values to variables in Bash?<\/strong><\/h2><p>Assigning a value to a Bash variable relies on a strict syntax where the assignment operator (<strong>=<\/strong>) connects a name to a value.<\/p><p>The most important rule is that you must not place spaces around the assignment operator.<\/p><p>Otherwise, the shell incorrectly interprets the variable name as a command and the value as an argument, resulting in an error.<\/p><p><strong>Direct assignment<\/strong> is the most fundamental method of defining a variable, where you explicitly map a static string to a variable name. This is typically used for setting constants or configuration paths that do not require dynamic computation.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\nserver_name=Apache-01\necho $server_name<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e7c272b0ff9\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"774\" height=\"70\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-variable-value-of-a-script.png\/public\" alt=\"Terminal outputs variable value of a script\" class=\"wp-image-136833\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-variable-value-of-a-script.png\/w=774,fit=scale-down 774w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-variable-value-of-a-script.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-variable-value-of-a-script.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-variable-value-of-a-script.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 774px) 100vw, 774px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>By default, <strong>B<\/strong><strong>ash treats numeric values<\/strong> <strong>as character strings<\/strong>. However, the shell can interpret these strings as integers when they are used within specific arithmetic contexts written inside double parentheses (<strong>(example)<\/strong>) or with the <strong>expr<\/strong> command.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\ninitial_value=10\n(( result = initial_value + 5 ))\necho $result<\/pre><p>If you want to assign values that<strong> <\/strong>contain spaces or special characters, use quotation marks. Without them, Bash interprets the first word as the variable value and subsequent words as separate commands or arguments.<\/p><p>Wrapping the text in quotes ensures the shell treats the entire sequence as a single unit of data.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\nerror_msg=\"File not found\"\necho $error_msg<\/pre><p>To ensure variables are parsed correctly, you must adhere to specific naming conventions.<\/p><p>Their names can only contain alphanumeric characters and underscores and cannot begin with a number. Bash is also case-sensitive, meaning<strong> PATH<\/strong>, <strong>Path<\/strong>, and <strong>path<\/strong> represent three distinct variables.<\/p><h2 class=\"wp-block-heading\" id=\"h-what-are-the-types-of-bash-variables\"><strong>What are the types of Bash variables?<\/strong><\/h2><p>Based on their accessibility and purpose within the system, variables in Bash are categorized into two types: user-defined and environment variables.<\/p><h3 class=\"wp-block-heading\" id=\"h-what-are-user-defined-variables\"><strong>What are user-defined variables?<\/strong><\/h3><p>User-defined variables are custom identifiers that users create to store temporary data within a Bash script or terminal session. These variables are local to the process that created them and are only valid when the session or script runs.<\/p><p>You create them by simply assigning a value to a new name. For example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\napp_dir=\"\/var\/www\/html\"\necho \"Deploying to: $app_dir\"<\/pre><p>In this <a href=\"\/tutorials\/bash-script-example\">Bash script example<\/a>, the <strong>app_dir <\/strong>variable is only valid for this particular script and the echo command, meaning other processes can&rsquo;t access it.<\/p><h3 class=\"wp-block-heading\" id=\"h-what-are-environment-variables\"><strong>What are environment variables?<\/strong><\/h3><p>In Bash, <a href=\"\/tutorials\/linux-environment-variables\">environment variables<\/a> are global settings whose data is accessible by accessible by child processes spawned from that shell. Since such variables are within the session, they can&rsquo;t be used by other users or already-running programs.<\/p><p>Bash environment variables are set using the export command and conventionally use uppercase letters as their names. For example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export VAR=value<\/pre><p>If you write this script with the same user and shell, it will automatically inherit the VAR variable&rsquo;s value:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\necho \"$VAR\"<\/pre><p>Here are some examples of Bash environment variables:<\/p><figure tabindex=\"0\" class=\"wp-block-table\"><table><tbody><tr><td><strong>Variable<\/strong><\/td><td><strong>Example<\/strong><\/td><td><strong>Function<\/strong><\/td><\/tr><tr><td>SHELL<\/td><td>\/bin\/bash<\/td><td>Defines the name of the shell used<\/td><\/tr><tr><td>PWD<\/td><td>\/root<\/td><td>Shows the current working directory<\/td><\/tr><tr><td>LOGNAME<\/td><td>root<\/td><td>Defines the logged-in user name<\/td><\/tr><tr><td>MOTD_SHOWN<\/td><td>pam<\/td><td>Defines the message of the day for the system<\/td><\/tr><tr><td>HOME<\/td><td>root<\/td><td>Home directory of the user<\/td><\/tr><tr><td>SSH_CONNECTION<\/td><td>185.185.185.185 54321 185.185.185.186 12<\/td><td>SSH connection information [user IP] [user port] [Linux machine IP] [Linux machine port]<\/td><\/tr><tr><td>TERM<\/td><td>xterm-256color<\/td><td>Defines the terminal type<\/td><\/tr><tr><td>USER<\/td><td>root<\/td><td>Name of the current user<\/td><\/tr><tr><td>SSH_CLIENT<\/td><td>185.185.185.185 54321 12<\/td><td>SSH client information [user IP] [user port] [Linux machine port]<\/td><\/tr><tr><td>PATH<\/td><td>\/usr\/local\/sbin<\/td><td>Defines the directories that bash will search to find a command<\/td><\/tr><tr><td>SSH_TTY<\/td><td>\/dev\/pts\/0=\/usr\/bin\/printenv<\/td><td>Displays the path to the device associated with the current shell or command<\/td><\/tr><\/tbody><\/table><\/figure><p>To check the environment variables in your system, use the <strong>printenv <\/strong>command. You can shorten the output by piping to <strong>less,<\/strong> like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">printenv | less<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e7c272b3668\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1326\" height=\"808\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/a-simplified-output-of-shell-environment-variables.png\/public\" alt=\"A simplified output of shell environment variables\" class=\"wp-image-136834\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/a-simplified-output-of-shell-environment-variables.png\/w=1326,fit=scale-down 1326w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/a-simplified-output-of-shell-environment-variables.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/a-simplified-output-of-shell-environment-variables.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/a-simplified-output-of-shell-environment-variables.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/a-simplified-output-of-shell-environment-variables.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1326px) 100vw, 1326px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-how-does-variable-scope-work-in-bash\"><strong>How does variable scope work in Bash?<\/strong><\/h2><p><strong>Variable scope <\/strong>defines the context in which a variable can be accessed or modified. It is crucial to prevent a variable in one function from accidentally overwriting data in the main script due to having the same name.<\/p><p>By default, all variables declared in a script are global, meaning they can be accessed and modified from anywhere in the script, including inside functions. To restrict a variable&rsquo;s visibility to a single function and prevent it from modifying the global one, use the local keyword.<\/p><p>For example, the following script has two variables called <strong>status<\/strong>. The global variable has the value of <strong>processing<\/strong>, while the local one inside the <strong>update_status()<\/strong> function is set to <strong>Done<\/strong>:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\n# Global variable\n\nstatus=\"Processing\"\n\nupdate_status() {\n    # Local variable with the same name\n    local status=\"Done\"\n    echo \"Inside function: $status\"\n}\n\necho \"Before function: $status\"\n\nupdate_status\n\necho \"After function: $status\"<\/pre><p>By default, printing the variable yields <strong>processing <\/strong>because it uses the global value. Meanwhile, the function where we set the local variable will output <strong>Done<\/strong>.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e7c272b57b2\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"620\" height=\"148\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-different-functions-with-local-and-global-variables.png\/public\" alt=\"Terminal outputs different functions with local and global variables\" class=\"wp-image-136835\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-different-functions-with-local-and-global-variables.png\/w=620,fit=scale-down 620w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-different-functions-with-local-and-global-variables.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-output-different-functions-with-local-and-global-variables.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-how-do-special-variables-work-in-bash\"><strong>How do special variables work in Bash?<\/strong><\/h2><p>Bash includes a set of reserved variables that automatically store data regarding the current shell environment. You cannot assign values to them manually as they inherit dynamic data from ongoing and completed processes, including their outputs and states.<\/p><p>These variables generally track script information and arguments:<\/p><ul class=\"wp-block-list\">\n<li><strong>$0<\/strong> &ndash; represents the name of the script file currently executing.<\/li>\n\n\n\n<li><strong>$#<\/strong> &ndash; stores the total number of arguments passed to the script.<\/li>\n\n\n\n<li><strong>$@<\/strong> &ndash; expands to all script arguments, treating each as a separate quoted string.<\/li>\n\n\n\n<li><strong>$<\/strong>* &ndash; expands to all script arguments, treating them as a single concatenated string.<\/li>\n<\/ul><p>Other variables monitor process control and execution status:<\/p><ul class=\"wp-block-list\">\n<li><strong>$$<\/strong> &ndash; displays the Process ID (PID) of the current shell script.<\/li>\n\n\n\n<li><strong>$!<\/strong> &ndash; displays the PID of the last command executed in the background.<\/li>\n\n\n\n<li><strong>$?<\/strong> &ndash; reports the exit status of the last executed command (0 for success, 1-255 for failure).<\/li>\n\n\n\n<li><strong>$_<\/strong> &ndash; holds the last argument of the previous command.<\/li>\n\n\n\n<li><strong>$-<\/strong> &ndash; shows the current flags and options set for the shell.<\/li>\n<\/ul><p>For example, this script utilizes special variables to display its own name, process ID, and the success status of a command.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\necho \"Script filename: $0\"\necho \"Current Process ID: $$\"\n\n# Attempt to list a non-existent file to generate an error\n\nls \/dummy_file 2&gt;\/dev\/null\n\n# Print the exit code of the previous command\n\necho \"Exit status (0=Success, Non-zero=Fail): $?\"<\/pre><p>Notice that the variable values are not hard-coded, but instead use a placeholder that changes dynamically based on the shell environment.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e7c272b74f1\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1036\" height=\"144\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-outputs-special-variable-values.png\/public\" alt=\"Terminal outputs special variable values\" class=\"wp-image-136836\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-outputs-special-variable-values.png\/w=1036,fit=scale-down 1036w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-outputs-special-variable-values.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-outputs-special-variable-values.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-outputs-special-variable-values.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/terminal-outputs-special-variable-values.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1036px) 100vw, 1036px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-how-to-export-and-unset-bash-variables\"><strong>How to export and unset Bash variables<\/strong><\/h2><p>Variables defined in a shell are not automatically passed to child processes, such as new scripts or shell sessions initiated from the current one.<\/p><p>If you want to include variables in the current shell for subsequent child processes, use the export command. For example, this script starts a new bash shell session and passes the API_KEY variable to it:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\n# Define and export a variable\n\nexport API_KEY=\"12345-ABCDE\"\n\n# Start a new child shell instance and print the variable\n\nbash -c 'echo \"Child shell sees: $API_KEY\"'<\/pre><p>In a real-world scenario, your script might start multiple child processes, and not all of them require the exported variable. For example, you may need to pass passwords or tokens to a specific process, but subsequent operations don&rsquo;t need them.<\/p><p>To prevent child processes from importing a variable, remove its value from memory by using the <strong>unset<\/strong> command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\n# Define and export a variable\n\nexport API_KEY=\"12345-ABCDE\"\n\n# Start a new child shell instance and print the variable\n\nbash -c 'echo \"Child shell sees: $API_KEY\"'\n\n# Remove variable from the memory for subsequent processes\n\nunset API_KEY\necho \"Parent shell after unset: $API_KEY\"\nbash -c 'echo \"Child shell sees: $API_KEY\"'<\/pre><p>In this example, the first child process can retrieve the variable value, but the subsequent processes can&rsquo;t. Note that the unset affects both the new and the current shell.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-use-quotes-and-substitution-in-bash\"><strong>How to use quotes and substitution in Bash<\/strong><\/h2><p>Bash provides mechanisms to control how text is interpreted, allowing you to choose between using literal strings or dynamic values.<\/p><h3 class=\"wp-block-heading\" id=\"h-when-to-use-single-vs-double-quotes\"><strong>When to use single vs double quotes<\/strong><\/h3><p>Single quotes (<strong>&lsquo;example&rsquo;<\/strong>) enforce literal interpretation of all characters, including <strong>$<\/strong>.<\/p><p>For example, the following script prints <strong>$HOME<\/strong> instead of its value because we enclose it in single quotes:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\necho 'The value is $HOME'\n\n# Output: The value is $HOME<\/pre><p>Double quotes (<strong>&ldquo;example&rdquo;<\/strong>) allow variables to change their values and pass command standard outputs within the string. For example, the following script passes the value of <strong>$HOME<\/strong> instead of printing the literal string:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\necho \"The value is $HOME\"\n\n# Output: The value is \/root<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e7c272b9079\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"1166\" height=\"106\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-passes-different-values-based-on-the-quotes.png\/public\" alt=\"Bash variable passes different values based on the quotes\" class=\"wp-image-136837\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-passes-different-values-based-on-the-quotes.png\/w=1166,fit=scale-down 1166w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-passes-different-values-based-on-the-quotes.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-passes-different-values-based-on-the-quotes.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-passes-different-values-based-on-the-quotes.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-passes-different-values-based-on-the-quotes.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1166px) 100vw, 1166px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-how-to-use-command-substitution\"><strong>How to use command substitution<\/strong><\/h3><p>Command substitution is used to insert the output of a command directly into variables in your script. To do this, enclose the command within the <strong>$( )<\/strong> syntax like so:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\nbackup_file=\"data_$(date +%F).tar.gz\"\necho \"Backing up to: $backup_file\"<\/pre><p>This script runs the <strong>date +%F<\/strong> command and passes its output as the value for the<strong> backup_file <\/strong>variable.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e7c272ba8ee\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"760\" height=\"72\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-subsitutes-its-value-using-a-command-output.png\/public\" alt=\"Bash variable substitutes its value using a command's output\" class=\"wp-image-136838\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-subsitutes-its-value-using-a-command-output.png\/w=760,fit=scale-down 760w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-subsitutes-its-value-using-a-command-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-subsitutes-its-value-using-a-command-output.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 760px) 100vw, 760px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>In legacy scripts, you may use backticks (<strong>`example`<\/strong>) to enclose your command. However, this is no longer recommended because it complicates nesting.<\/p><h2 class=\"wp-block-heading\" id=\"h-common-examples-of-using-bash-variables-in-scripts\"><strong>Common examples of using Bash variables in scripts<\/strong><\/h2><p>Given their flexibility, you can use Bash variables in Bash scripts differently depending on what you want to achieve. You can also combine them with different Bash mechanisms to further enhance your script functionality.<\/p><p>For example, you can combine bash variables with arithmetic operations and<strong> if<\/strong> conditionals, like in this example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\ncount=3\nif (( count &gt; 2 )); then\n    echo \"Count is greater than 2\"\nfi<\/pre><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e7c272bc411\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"722\" height=\"66\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/the-if-statement-bash-variable-output.png\/public\" alt=\"The output of a script combining an if statement with a bash variable\" class=\"wp-image-136839\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/the-if-statement-bash-variable-output.png\/w=722,fit=scale-down 722w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/the-if-statement-bash-variable-output.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/the-if-statement-bash-variable-output.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 722px) 100vw, 722px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>The variable <strong>count<\/strong> holds a number, and the conditional uses its value to decide whether to run the echo command. This demonstrates how variables provide data that Bash can use to make logical decisions.<\/p><p>The<strong> if<\/strong> statement in Bash also commonly uses square brackets to specify the conditions that the function will check. For example, this script prints a message if a text file is found within the variable:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\nfile=\"report.txt\"\nif [[ $file == *.txt ]]; then\n    echo \"It is a text file\"\nfi<\/pre><p>While variables <strong>mustn&rsquo;t <\/strong>have spaces around them, the condition inside the square brackets of the <strong>if<\/strong> statement <strong>should<\/strong>. Given their different behaviors, pattern errors commonly occur when combining these bash mechanics together.<\/p><p>Aside from combining it with an<strong> if<\/strong> statement, you can also use a variable to store a <a href=\"\/tutorials\/bash-array\">Bash array<\/a>, making it easy to loop through its items within a script. Here&rsquo;s an example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\nnums=(1 2 3)\nfor n in \"${nums[@]}\"; do\n    echo \"$n\"\ndone<\/pre><p>This script sets a variable that contains an array of numbers, iterates through them using the <a href=\"\/tutorials\/bash-for-loop\">for loop<\/a>, and passes each item into the echo command.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69e7c272bdcfe\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"540\" height=\"146\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-array.png\/public\" alt=\"The output of a script combining a bash variable and an array\" class=\"wp-image-136840\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-array.png\/w=540,fit=scale-down 540w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-array.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/bash-variable-array.png\/w=150,fit=scale-down 150w\" sizes=\"(max-width: 540px) 100vw, 540px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>When working with an array, it&rsquo;s crucial to enclose your variable in<strong> curly braces ({ }) <\/strong>to prevent Bash from reading the variable as<strong> nums[@]<\/strong>. It&rsquo;s also helpful if you want to combine variable values, like in this example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">dirpath=\"\/var\/www\/site\"\nbackup=\"${dirpath}_backup\"<\/pre><p>Without curly braces, Bash would interpret the backup variable as <strong>dirpath_backup<\/strong> instead of <strong>\/var\/www\/site_backup<\/strong>.<\/p><p>Another common mistake when using variables is not enclosing them with quotes, especially when passing them to functions. The good formatting looks like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var=\"Value of the var\"\necho \"$var\"<\/pre><p>If the value contains a whitespace, Bash will interpret it as multiple arguments instead of a single string. If you use <strong>$var <\/strong>without quotation marks, the <strong>echo<\/strong> command will read <strong>Value<\/strong>,<strong> of<\/strong>, <strong>the<\/strong>, and <strong>var <\/strong>as individual strings.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-can-you-build-scripts-using-bash-variables\"><strong>How can you build scripts using Bash variables?<\/strong><\/h2><p>Bash variables are the building blocks for creating flexible, automated scripts. By storing values such as filenames, user input, or lists of items, you can create dynamic scripts that help automate complex Linux server administration tasks.<\/p><p>For example, you can use a variable to store a directory path, then loop over all files in that directory and perform an action on each file, like moving, renaming, or backing them up.<\/p><p>Also, you can capture user input in a variable and make decisions based on that input, such as choosing which service to restart or which file to process.<\/p><p>Variables can even hold arrays or lists, letting you handle multiple items efficiently without hardcoding anything.<\/p><p>In addition, you can combine values from multiple variables or attach strings to them, which is helpful when working with dynamic data like process outputs. To learn more, check out our guide on <a href=\"\/tutorials\/bash-concatenate-strings\">concatenating strings in Bash<\/a>.<\/p><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Bash variables are named storage units that hold data values for reference and manipulation within the Linux command line environment. [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/bash-variables\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to use bash variables: Examples, types, and syntax","rank_math_description":"Learn how to assign, export, and use bash variables with examples, including special and environment variables for your shell scripts.","rank_math_focus_keyword":"bash variables","footnotes":""},"categories":[22648,22644],"tags":[],"class_list":["post-65790","post","type-post","status-publish","format-standard","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/bash-variables","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/variables-en-bash","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/bash-variables","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/bash-variables","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/bash-variables","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/variables-en-bash","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/variables-en-bash","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/variables-en-bash","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/bash-variables","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/bash-variables","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/bash-variables","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/bash-variables","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/65790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=65790"}],"version-history":[{"count":27,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/65790\/revisions"}],"predecessor-version":[{"id":136841,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/65790\/revisions\/136841"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=65790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=65790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=65790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}