{"id":57982,"date":"2024-01-18T19:14:05","date_gmt":"2024-01-18T19:14:05","guid":{"rendered":"https:\/\/www.askpython.com\/?p=57982"},"modified":"2025-04-10T20:46:47","modified_gmt":"2025-04-10T20:46:47","slug":"linux-export-env-variables-with-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/linux-export-env-variables-with-python","title":{"rendered":"How to use export with Python on Linux"},"content":{"rendered":"\n<p>The <strong>&#8216;export&#8217;<\/strong> command in Linux is typically used to set an environment variable. Environment variables are dynamic values that can affect the behavior of processes and programs running in the operating system. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>The &#8216;export&#8217; command helps with setting environment variables and influencing the behavior of ongoing processes. This command follows the syntax &#8216;export VARIABLE_NAME=&#8221;value&#8221;&#8216;, where spaces are not allowed between the variable name and value. Additionally, the &#8216;env&#8217; and &#8216;echo&#8217; commands are used to view these variables. Integrating &#8216;export&#8217; with Python scripts involves creating a script, setting environment variables via a shell script, making the script executable, and executing it. This process is instrumental in managing dynamic values essential for process environments in Linux<\/em><\/p>\n<\/blockquote>\n\n\n\n<p><em>Also read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/python-alpine-linux\" data-type=\"post\" data-id=\"53715\">Installing Python in Alpine Linux<\/a><\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The basic syntax of the &#8216;<strong>export<\/strong>&#8216; command<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nexport VARIABLE_NAME=&quot;value&quot;\n<\/pre><\/div>\n\n\n<p>This is the basic syntax of the &#8216;<strong>export<\/strong>&#8216; command. &#8216;<strong>V1<\/strong>&#8216; is your variable name and &#8216;<strong>value<\/strong>&#8216; is your variable value. Here, you can&#8217;t add a space between the variable name and value. <\/p>\n\n\n\n<p>Here is one example of an &#8216;<strong>export<\/strong>&#8216; command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nexport V1=&quot;Hello, World!&quot;\n<\/pre><\/div>\n\n\n<p>In the above example, we set a value of a variable as &#8216;<strong>Hello, World!<\/strong>&#8216; with variable name &#8216;<strong>V1.<\/strong>&#8216; Now the value Hello, World! is stored in variable V1.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"27\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_command.png\" alt=\"Export Command\" class=\"wp-image-57995\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_command.png 527w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_command-300x15.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/figure>\n\n\n\n<p>Now you can see a list of environment variables by using the command &#8216;<strong>env<\/strong>&#8216; or &#8216;<strong>echo<\/strong>&#8216; with &#8216;<strong>$VARIABLE_NAME<\/strong>&#8216;.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\necho $VARIABLE_NAME\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"407\" height=\"52\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/echo.png\" alt=\"Echo\" class=\"wp-image-57996\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/echo.png 407w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/echo-300x38.png 300w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/figure>\n\n\n\n<p>Here we use the &#8216;<strong>echo $V1<\/strong>&#8216; command line to get the value of variable V1 and the output is <strong>&#8220;Hello, World!&#8221;<\/strong>.<\/p>\n\n\n\n<p>So this is the basics about export command in Linux.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>Also read:<a href=\"https:\/\/www.askpython.com\/python\/examples\/downgrade-python-37-to-36\" data-type=\"post\" data-id=\"57216\"> Downgrade From Python 3.7 to 3.6 on Windows, MacOS, and Linux<\/a><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Integrating Export Command with Python Scripts in Linux<\/h2>\n\n\n\n<p>The export command with Python in Linux is typically used to refer to setting environment variables. Environment variables are key-value pairs that store information about the environment in which the process runs. <br>Here are steps to use export with Python:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a Python script <\/li>\n\n\n\n<li>Create a shell script to set environment variables using export<\/li>\n\n\n\n<li>Make the shell script executable<\/li>\n\n\n\n<li>Run the shell script<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Python Script for Environment Variables<\/h3>\n\n\n\n<p>Create a Python script. You can create a Python script using vi or nano text editor. <\/p>\n\n\n\n<p><strong>With vi editor:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nvi python_script.py\n<\/pre><\/div>\n\n\n<p><strong>With nano editor:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnano python_script\n<\/pre><\/div>\n\n\n<p>Here we create a Python script with vi editor. For example, we create a Python script for the addition of two numbers named example.py. To start typing the Python script, press&#8217;<strong> i <\/strong>&#8216;to get into insert mode. Now you can start typing your Python script.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nimport os\n\nn1 = int(os.getenv(&quot;N1&quot;, 0))\nn2 = int(os.getenv(&quot;N2&quot;, 0))\n\nsum = n1 + n2\n\nprint(f&quot;The sum is: {sum}&quot;)\n<\/pre><\/div>\n\n\n<p>Once done with typing, press Esc to exit insert mode. To save and exit type &#8216;:wq!&#8217; and press Enter.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"473\" height=\"263\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_eg.png\" alt=\"Export Eg\" class=\"wp-image-57997\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_eg.png 473w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_eg-300x167.png 300w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/figure>\n\n\n\n<p>In the above example, by using &#8216;<strong>import os<\/strong>&#8216;, we import the &#8216;<strong>os<\/strong>&#8216; module. The &#8216;<strong>os<\/strong>&#8216; module in Python includes various functions for file operations, environment variables, process management, and many more. <\/p>\n\n\n\n<p><em><strong>&#8216;os.getenv()&#8217;<\/strong> is one of the function of the &#8216;<strong>os<\/strong>&#8216; module. This is used to retrieve the values of environment variables as <strong>N1<\/strong> and <strong>N2<\/strong>. n1 and n2 stored the retrieved values of environment variables. <\/em><\/p>\n\n\n\n<p>If the environment values are not set, the default value 0 is used. The &#8216;sum = n1 + n2&#8217;, calculates the sum of two integers obtained from the environment variables. Finally, the last line prints the result of the addition.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a shell script, here we will use &#8216;<strong>export<\/strong>&#8216; to set environment variables. For example, create a shell script named &#8216;<strong>script.sh<\/strong>&#8216; where we set environment variables with their values.  <\/li>\n<\/ul>\n\n\n\n<p>To create a shell use the following command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nvi script.sh\n<\/pre><\/div>\n\n\n<p>Press&#8217;<strong> i <\/strong>&#8216; to enter insert mode and then start typing your shell script. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\n\nexport N1=5\nexport N2=7\n\npython3 example.py\n<\/pre><\/div>\n\n\n<p>After done typing, press Esc to exit insert mode, and to save and exit type &#8216;:wq!&#8217; and press Enter. <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"421\" height=\"207\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_bash.png\" alt=\"Export Bash\" class=\"wp-image-57998\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_bash.png 421w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_bash-300x148.png 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/figure>\n\n\n\n<p>The first line, &#8216;<strong>#!\/bin\/bash<\/strong>&#8216; is called a shebang. Here by using this line, it specifies that the bash shell should be used. &#8216;<strong>export N1=5 <\/strong>&#8216; and &#8216;<strong>export N2=7<\/strong>&#8216;, these line uses the <strong>export<\/strong> command to set the environment variables,<strong> N1<\/strong> and <strong>N2<\/strong> with values <strong>5<\/strong> and <strong>7<\/strong> respectively. The last line &#8216;<strong>python3 example.py<\/strong>&#8216;, invokes the Python 3 interpreter to run the script named example.py. You can replace example.py with the name of your Python script.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now make the shell script executable using the following command:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nchmod +x script.sh\n<\/pre><\/div>\n\n\n<p>This command grants execute permissions to the user. The <strong>script.sh<\/strong> is the name of the script. Here you can replace script.sh with your actual script.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The final step is to run the shell script. To run the shell script use the following command:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n.\/script.sh\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"51\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_op.png\" alt=\"Export Op\" class=\"wp-image-58002\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_op.png 468w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/export_op-300x33.png 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/figure>\n\n\n\n<p>The output is 12 which is an addition of 5 and 7.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Here we use export with Python in Linux with some steps. First, we create a Python script and then a shell script using a vi text editor. You can use nano editor as well. Then we make the shell script executable and finally, we run the shell script. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">References:<\/h4>\n\n\n\n<p><a href=\"https:\/\/pythonhint.com\/post\/2676691856440983\/how-to-use-export-with-python-on-linux\" target=\"_blank\" rel=\"noopener\">https:\/\/pythonhint.com\/post\/2676691856440983\/how-to-use-export-with-python-on-linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8216;export&#8217; command in Linux is typically used to set an environment variable. Environment variables are dynamic values that can affect the behavior of processes and programs running in the operating system. The &#8216;export&#8217; command helps with setting environment variables and influencing the behavior of ongoing processes. This command follows the syntax &#8216;export VARIABLE_NAME=&#8221;value&#8221;&#8216;, where [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":64085,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-57982","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=57982"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57982\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/64085"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=57982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=57982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=57982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}