{"id":169872,"date":"2021-02-03T16:00:20","date_gmt":"2021-02-03T13:00:20","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/exporting-variables-in-bash-the-why-and-how\/"},"modified":"2021-02-03T16:00:20","modified_gmt":"2021-02-03T13:00:20","slug":"exporting-variables-in-bash-the-why-and-how","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/exporting-variables-in-bash-the-why-and-how\/","title":{"rendered":"#Exporting Variables in Bash: the Why and How"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a39ba958b6d1\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" 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: #dd3333;color:#dd3333\" 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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a39ba958b6d1\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/exporting-variables-in-bash-the-why-and-how\/#Exporting_Variables_in_Bash\" >Exporting Variables in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/exporting-variables-in-bash-the-why-and-how\/#What_Is_a_Bash_subshell\" >What Is a Bash subshell?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/exporting-variables-in-bash-the-why-and-how\/#Exporting_Variables_to_Subshells\" >Exporting Variables to Subshells<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/exporting-variables-in-bash-the-why-and-how\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Exporting Variables in Bash: the Why and How&#8221;<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4038\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/f1fee0a0a83b16d260ba2e862cb46eec\/p\/uploads\/2017\/07\/add8ac45.png\" alt=\"Bash Shell\" width=\"1400\" height=\"600\" \/><\/p>\n<p>Exporting Variables in Bash; why is it done, and how can it be done well? This article will teach you about subshells, and will exemplify Bash variable exporting therewith, showing you how to export variables, and how to avoid mistakes.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Exporting_Variables_in_Bash\"><\/span>Exporting Variables in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When developing more complex <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>s in Bash, which employ for example subshell and multi-threaded and\/or background process setups, one will regularly have to pass variables from the master shell to a subshell. It is exactly in these types of situations that exporting shell variables becomes of interest.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_Is_a_Bash_subshell\"><\/span>What Is a <i>Bash subshell<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A Bash subshell, or a child shell, is nothing more then a Bash command line interpreter (in other words, the Bash shell) restarted from within itself. The original process has thus become the master process, and the second process to be started (the one started inside the master, or fist, process) is going to be the subshell. If the master process\/shell terminates, so will the subshell running within it.<\/p>\n<p>Let&#8217;s do this very simply as follows:<\/p>\n<pre>bash\necho $$\nps -ef | grep -v grep | grep your_process_id\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9085 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/b31f8d033ad3e71e16a3e45219fa3f86\/p\/uploads\/2021\/01\/67bd7118.png\" alt=\"Simple example of starting a subshell in Bash\" width=\"491\" height=\"223\" \/><\/p>\n<p>Nothing seemed to happen when we typed <code>bash<\/code> in the terminal and pressed enter. However, what happened in the background was that a new process (the subshell) was started and we im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely <i>entered into<\/i> that subshell.<\/p>\n<p>We can verify the same by checking what sort of processes are live. First, we discover the PID (process ID) of our current (sub-)shell. We can do so by inspecting the <code>$$<\/code> variable with <code>echo<\/code>. The PID is 362827. Next, we can our process list using <code>ps -ef<\/code> and we exclude the grep process from the list with the grep <code>-v<\/code> negation in <code>| grep -v grep<\/code>.<\/p>\n<p>We discover that the PPID (the parent process ID) of our subshell with PID 362827 is 362815 (first green box). The PID (the process ID of the program mentioned at the end of the line) is always displayed on the left, whereas the parent process ID (PPID) is always displayed on the right. This allows us to backtrack as far as we can or like to go.<\/p>\n<p>We thus discover that the 362815 process (the main\/parent process which hosts the 362827 subshell) is owned by process 13185 (white highlight), and this process is identified by the last <code>ps<\/code> as the terminal window which was started in the desktop environment. This in turn is owned by a much earlier started process 2184 (process name not shown here) etc.<\/p>\n<p>The total (visible) hierarchy is thus <strong>2184<\/strong> &gt; <strong>13185<\/strong> (terminal window) &gt; <strong>362815<\/strong> (master\/main shell inside the terminal window) &gt; <strong>362827<\/strong> (the subshell started from within the Bash shell with process ID 362815). For a more visible representation of this hierarchy, you may like to checkout the <code>pstree<\/code> command\/utility, which may require installing on your operating system.<\/p>\n<p>To learn more about Unix PID&#8217;s, see What Are Unix PIDs and How Do They Work? by Anthony Heddings.<\/p>\n<p>It is import to understand how the hierarchy of Bash works as it helps us understand why and where the export of variables becomes interesting, and often necessary.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Exporting_Variables_to_Subshells\"><\/span>Exporting Variables to Subshells<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are many ways to create a subshell. One way is to simply start a subshell as shown above by typing <code>bash<\/code> and executing further command line based commands. Another is by starting a GNU screen session (see How to Use the GNU Screen Utility in Linux if you like to learn more about GNU Screen).<\/p>\n<p>You can also use the <code>$(subshell code goes here)<\/code> subshell idiom directly from within a command line and\/or your Bash scripts. Finally, one can start a subshell simply by placing a process into background. To learn more about background process management, see <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/linuxconfig.org\/bash-background-process-management\">Bash Background Process Management<\/a>.<\/p>\n<p>So with all of these methods to initiate subshells, how can one easily pass variables from one shell to another? Sure, one could store the variables in a file (in any format), but this will increase the number of I\/O operations, and the complexity of the Bash code that handles this writing and reading of variables.<\/p>\n<p>There is an easier way. You may think about it like global variables in other coding languages. Yes, perhaps global variables tend to be shunned or frowned upon, but remember we are dealing with separate processes here.<\/p>\n<p>In any multi-thread\/multi-process programming in any language, there would still be a supervisory module\/code which would pass variables back and forth between the threads. Such variables could be considered <i>global<\/i> also.<\/p>\n<p>The syntax for the export command is very simple. Simply prefix the variable assignment with the <code>export<\/code> command:<\/p>\n<pre>export A=1\nB=2\nbash\necho ${A}\necho ${B}\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9086\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/650381687c8d5f3b81b02f817663ad43\/p\/uploads\/2021\/01\/12793dc5.png\" alt=\"Exporting of a variable\" width=\"213\" height=\"138\" \/><\/p>\n<p>Here we start a subshell, and show how the variable <code>A<\/code> was correctly passed to the subshell by using the <code>export<\/code> command when defining it. We also see how the variable <code>B<\/code> has not transferred to the subshell, as it was defined without using <code>export<\/code>.<\/p>\n<p>The export property is a specific property which can be turned on and off for a given variable. It is easily turned on by using the <code>export<\/code> command prefix when defining the variable.<\/p>\n<p>Once such a property is set, it remains active. This is something to watch out for when coding as it can lead to confusing results if one does not know this operation specific. It is easy to assume one is &#8220;reusing&#8221; a variable with the same name, or even to accidentally use a similar variable name from different scripts in the subshell etc. Let&#8217;s explore an example:<\/p>\n<pre>export C=1\nexport D=\nD=1\nbash\necho ${C}\necho ${D}\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9087\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/2bf2678776e93fca3df0209723a6ac7b\/p\/uploads\/2021\/01\/d15c4a3e.png\" alt=\"Shows the stability of the export property of a variable\" width=\"218\" height=\"134\" \/><\/p>\n<p>In this example we see that the <code>D<\/code> variable retains it&#8217;s export property, even when re-assigned without the explicit <code>export<\/code> command, and it&#8217;s value is correctly passed to the subshell.<\/p>\n<p>The export property can also be removed from\/turned off for a variable, by using the <code>-n<\/code> option to <code>export<\/code>. However, to be effective, this has to be done from the master\/main shell, and not the subshell:<\/p>\n<pre>export -n D\necho ${D}\nexit\necho ${D}\nbash\necho ${D}\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9089\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/9136c1ddc8d42ededbfaf77e68dbaedd\/p\/uploads\/2021\/01\/00ab4a6c-1.png\" alt=\"One cannot remove the export property from a variable from within a subshell if that property was set by the master\/parent shell\" width=\"180\" height=\"195\" \/><\/p>\n<p>This example continues on from the last one (i.e. still from within the active subshell). We try and remove the <i>export<\/i> property from the <code>D<\/code> variable, and subsequently exit the subshell using <code>exit<\/code>. We see that on returning to the main shell that our <code>D<\/code> variable has retained it&#8217;s value, and that value remains retained when re-entering a new subshell. Thus, as seen, an export property cannot be removed from a variable from within a subshell if that variable was given an export property in the main\/master shell.<\/p>\n<p>This is alike to other Bash coding methods: the subshell cannot affect the master\/main shell. This also makes the most sense from a security perspective in many cases, and security is likely the reason the Bash developers chose to implement it this way. Let&#8217;s take it one step further now and remove the <i>export<\/i> property from the main\/parent shell:<\/p>\n<pre>exit\nexport -n D\nbash \necho ${D}\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9090\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ee532352fd9a5f119dedc124ebe032be\/p\/uploads\/2021\/01\/0f4be8eb.png\" alt=\"The parent\/master shell can remove the export property from the variable\" width=\"146\" height=\"121\" \/><\/p>\n<p>Here we exit the subshell just created in the last example, and then unset the <i>export<\/i> property from the <code>D<\/code> variable. Next we re-enter a subshell alike to our last example, and we can see that our <code>D<\/code> variable is empty, as it was no longer exported to the subshell. You can also run <code>export -p<\/code> to see all export variables. The variables you exported are likely near the top of this list.<\/p>\n<pre>export -p | head -n1\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9091\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/a9d441c3428429690de24fda123b3914\/p\/uploads\/2021\/01\/bae4f3bc.png\" alt=\"Using export -p to print exported variables\" width=\"223\" height=\"52\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Having explored subshells and their connection with exporting Bash variables, we next exemplified Bash variable exporting. We looked at various ways to export variables, as well as a method to clear the <i>export<\/i> property from variables. We also saw a gotcha which can arise if you reuse variable names. <strong>Enjoy Exporting!<\/strong><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9084\/exporting-variables-in-bash-the-why-and-how\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Exporting Variables in Bash: the Why and How&#8221; Exporting Variables in Bash; why is it done, and how can it be done well? This article will teach you about subshells, and will exemplify Bash variable exporting therewith, showing you how to export variables, and how to avoid mistakes. Exporting Variables in Bash When developing more&#8230;<\/p>\n","protected":false},"author":1,"featured_media":169873,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/add8ac45.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-169872","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/169872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=169872"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/169872\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/169873"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=169872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=169872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=169872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}