{"id":16071,"date":"2019-03-19T14:39:37","date_gmt":"2019-03-19T14:39:37","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=16071"},"modified":"2025-04-28T07:54:09","modified_gmt":"2025-04-28T07:54:09","slug":"linux-environment-variables","status":"publish","type":"post","link":"\/uk\/tutorials\/linux-environment-variables","title":{"rendered":"How to Set and List Linux Environment Variables"},"content":{"rendered":"<p>Linux environment variables are key-value pairs defining your system&rsquo;s shell session and program behavior. They let system administrators easily share and distinguish configurations between applications and users.<\/p><p>Admins can modify them using Linux commands to tweak their server settings based on the hosted applications&rsquo; needs. Depending on the tasks, you can also set up temporary or permanent environment variables.<\/p><p>To help you understand them, this article will explain Linux environment variables and how to manage them. We will also explore their benefits and best practices for configuring them for efficient system administration.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet-1024x283.png\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-1536x425.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-300x83.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-150x41.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-768x212.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>\n\n\n\n\n<div class=\"protip\">\n                    <h2 class=\"featured-snippet title\">What Are Environment Variables in Linux?<\/h2>\n                    <p><br>\nEnvironment variables are dynamic values determining the behavior of shells and processes in a Linux system. Using them, admins can easily set default settings and share configurations between applications for more efficient management.<br>\nUsers can view all the environment variables with their values using the <strong>printenv<\/strong> command and set new ones with the <strong>export<\/strong> utility.<\/p>\n                <\/div>\n\n\n\n<\/p><p>Linux distributions have environment and shell variables, which function similarly but have different scopes.<\/p><p>Environment variables have a global scope, accessible in all shell sessions, child processes, and other programs. They may apply to a specific user or all accounts within a system.<\/p><p>Meanwhile, shell or local environment variables only apply to the current session in which they are created. You can&rsquo;t use them on sub-shells, other processes, or different accounts.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-manage-linux-environment-variables\">How to Manage Linux Environment Variables<\/h2><p>In this section, we will explain how to check environment variables in Linux. For a remote system like a <a href=\"\/uk\/tutorials\/what-is-vps-hosting\">virtual private server<\/a> (VPS), connect using an <a href=\"\/uk\/tutorials\/how-to-use-putty-ssh\">SSH client like PuTTy<\/a> or Terminal.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>Hostinger offers a <strong>Browser<\/strong> terminal feature for executing commands and managing system variables in Linux servers via web browsers. In addition to convenience, this tool is useful when users disable SSH for security reasons.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\">How to List Environment Variables in Linux<\/h3><p>To list all environment variables in Linux, run the <strong>printenv <\/strong>command without any options or flags:<\/p><pre class=\"wp-block-preformatted\">printenv<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/The-printenv-command-lists-all-system-environment-variables.png\"><img decoding=\"async\" width=\"1024\" height=\"360\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/The-printenv-command-lists-all-system-environment-variables-1024x360.png\" alt=\"The printenv command lists all system environment variables\" class=\"wp-image-100319\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-printenv-command-lists-all-system-environment-variables.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-printenv-command-lists-all-system-environment-variables-300x105.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-printenv-command-lists-all-system-environment-variables-150x53.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-printenv-command-lists-all-system-environment-variables-768x270.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>By default, Terminal will print all the environment variables and their values, making the output very long. Add the <strong>less <\/strong>modifier to simply the list:<\/p><pre class=\"wp-block-preformatted\">printenv | less<\/pre><p>You can also use <strong>env<\/strong>, which<strong> <\/strong>works similarly to <strong>printenv<\/strong>. Meanwhile, the <strong>set <\/strong>command prints shell and environment variables.<\/p><p>If you want to show only Linux shell variables of the current user, use <strong>grep <\/strong>to filter the output.<\/p><p>In <a href=\"\/uk\/tutorials\/best-linux-distro\">popular Linux distributions<\/a>, some of the most common environment variables include:<\/p><ul class=\"wp-block-list\">\n<li><strong>PWD<\/strong> &ndash; the current working directory.<\/li>\n\n\n\n<li><strong>USER <\/strong>&ndash; the currently logged-in user account.<\/li>\n\n\n\n<li><strong>SHELL<\/strong> &ndash; the current user&rsquo;s shell location.<\/li>\n\n\n\n<li><strong>HOME<\/strong> &ndash; the user&rsquo;s home directory.<\/li>\n\n\n\n<li><strong>EDITOR<\/strong> &ndash; the system&rsquo;s default file editor.<\/li>\n\n\n\n<li><strong>PATH<\/strong> &ndash; the location of all system executable files.<\/li>\n<\/ul><p>These special variables have pre-defined purposes and roles. However, you can set user-defined ones to configure your system based on your needs.<\/p><h3 class=\"wp-block-heading\">How to Print Environment Variables in Linux<\/h3><p>Use the <a href=\"\/uk\/tutorials\/echo-command-linux\"><strong>echo<\/strong> command<\/a> to print a single environment variable value. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\"><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">echo<\/code> $VARIABLE<\/pre><p>Consider the capitalization since this command is case-sensitive. For example, enter the following to retrieve the <strong>PATH <\/strong>environment<strong> <\/strong>variable value:<\/p><pre class=\"wp-block-preformatted\">echo $PATH<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/The-echo-command-prints-the-current-value-of-the-PATH-variable.png\"><img decoding=\"async\" width=\"1024\" height=\"43\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/The-echo-command-prints-the-current-value-of-the-PATH-variable-1024x43.png\" alt=\"The echo command prints the current value of the PATH variable\" class=\"wp-image-100320\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-echo-command-prints-the-current-value-of-the-PATH-variable.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-echo-command-prints-the-current-value-of-the-PATH-variable-300x12.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-echo-command-prints-the-current-value-of-the-PATH-variable-150x6.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-echo-command-prints-the-current-value-of-the-PATH-variable-768x32.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Alternatively, use the printenv command with the variable you want to look up. For instance, run the following to check the <strong>HOME<\/strong> environment variable value:<\/p><pre class=\"wp-block-preformatted\">printenv HOME<\/pre><p>You can also apply the <strong>grep<\/strong> command to check if an entry exists. For example, run the command below to search the <strong>USER<\/strong> variable:<\/p><pre class=\"wp-block-preformatted\">printenv | grep USER<\/pre><p>To query multiple environment variables&rsquo; values, list them after the <strong>printenv<\/strong> command like:<\/p><pre class=\"wp-block-preformatted\">printenv VAR1 VAR2<\/pre><h3 class=\"wp-block-heading\">How to Set Environment Variables in Linux<\/h3><p>Use the <strong>export <\/strong>command to temporarily set a global environment variable in a Linux operating system. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">export VAR=\"value\"<\/pre><p>Replace <strong>VAR <\/strong>with the variable name you want to modify and <strong>value <\/strong>with its new value. For example, run the following command to set a new user&rsquo;s home directory:<\/p><pre class=\"wp-block-preformatted\">export HOME=\"\/etc\/another\/path\"<\/pre><p>The output should change if you print the<strong> HOME <\/strong>variable value using the <strong>echo <\/strong>command. You can also set multiple values using a semicolon like the following:<\/p><pre class=\"wp-block-preformatted\">export VAR=\"value1:value2\"<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/The-export-command-sets-two-values-for-an-environment-variable.png\"><img decoding=\"async\" width=\"949\" height=\"80\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/The-export-command-sets-two-values-for-an-environment-variable.png\" alt=\"The export command sets two values for an environment variable\" class=\"wp-image-100323\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-export-command-sets-two-values-for-an-environment-variable.png 949w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-export-command-sets-two-values-for-an-environment-variable-300x25.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-export-command-sets-two-values-for-an-environment-variable-150x13.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-export-command-sets-two-values-for-an-environment-variable-768x65.png 768w\" sizes=\"(max-width: 949px) 100vw, 949px\" \/><\/a><\/figure><\/div><p>Remember, this command only applies the new global variable for the current environment. Moreover, the changes will reset after you reboot the system.<\/p><p>To set a local environment variable, specify the value directly into the command-line interface using this syntax:<\/p><pre class=\"wp-block-preformatted\">var=\"value\"<\/pre><p>Use all lowercase letters to distinguish it from the global variable and avoid overlaps. If you enter a sub-shell using the <strong>bash <\/strong>command, printing the value will give an empty output.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>We recommend using the temporary environment variables for tasks like debugging or testing since it won&rsquo;t affect other shell processes.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\">How to Make Persistent Environment Variables in Linux<\/h3><p>Modify the shell-specific configuration files to make your environment variables persistent. However, the file to edit depends on whether you want to change user-specific or system-wide configuration.<\/p><p><strong>User-specific Environment Variables<\/strong><\/p><p>Edit the <strong>~\/.bashrc <\/strong>to only set a permanent environment variable for the current user. To do so, open it using a text editor like <a href=\"\/uk\/tutorials\/how-to-install-and-use-nano-text-editor\">nano<\/a>:<\/p><pre class=\"wp-block-preformatted\">sudo nano ~\/.bashrc<\/pre><p>Add the key-value pair at the beginning or bottom of the file for better organization. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">export VAR=\"VALUE\"<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/New-environment-variables-in-the-bashrc-file.png\"><img decoding=\"async\" width=\"1024\" height=\"215\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/New-environment-variables-in-the-bashrc-file-1024x215.png\" alt=\"New environment variables in the bashrc file\" class=\"wp-image-100326\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/New-environment-variables-in-the-bashrc-file.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/New-environment-variables-in-the-bashrc-file-300x63.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/New-environment-variables-in-the-bashrc-file-150x32.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/New-environment-variables-in-the-bashrc-file-768x162.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Press <strong>Ctrl+X<\/strong>, <strong>Y<\/strong>, and <strong>Enter<\/strong> to close nano and save the changes. Refresh the current shell session to apply the new configuration using this command:<\/p><pre class=\"wp-block-preformatted\">source ~\/.bashrc<\/pre><p>Instead of<strong> ~\/.bashrc<\/strong>, modify the <strong>~\/.profile <\/strong>file if you want to set variables for login shells.<\/p><p>Note that you can also modify the shell functions through the <strong>~.\/bashrc<\/strong> file.<\/p><p><strong>System-wide Environment Variables<\/strong><\/p><p>To set permanent environment variables that apply to all users within an operating system, edit the <strong>\/etc\/environment <\/strong>file using a text editor:<\/p><pre class=\"wp-block-preformatted\">sudo nano \/etc\/environment<\/pre><p>Add the key-value pairs to the file using the basic syntax without <strong>export<\/strong>:<\/p><pre class=\"wp-block-preformatted\">VAR=\"VALUE\"<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/The-new-environment-variable-in-the-environment-configuration-file.png\"><img decoding=\"async\" width=\"1024\" height=\"75\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/The-new-environment-variable-in-the-environment-configuration-file-1024x75.png\" alt=\"The new environment variable in the environment configuration file\" class=\"wp-image-100328\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-new-environment-variable-in-the-environment-configuration-file.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-new-environment-variable-in-the-environment-configuration-file-300x22.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-new-environment-variable-in-the-environment-configuration-file-150x11.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/The-new-environment-variable-in-the-environment-configuration-file-768x56.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Save the file and close <strong>nano<\/strong>. To apply the changes, re-login your current user or restart the system using the <strong>reboot <\/strong>command. The configuration will load whenever you enter the bash login shell<\/p><p>In addition to the <strong>\/etc\/environment <\/strong>file, you can add the environment variable to the <strong>\/etc\/profile.d<\/strong> directory. To do so, navigate to the folder using this command:<\/p><pre class=\"wp-block-preformatted\">sudo cd \/etc\/profile.d<\/pre><p>Create a new shell script file to store your new global variable using the<strong> nano<\/strong> text editor:<\/p><pre class=\"wp-block-preformatted\">sudo nano global_env.sh<\/pre><p>Replace <strong>global_env <\/strong>with your desired name, preferably a descriptive one, to simplify identification. In <strong>nano<\/strong>, write your environment variables using this syntax:<\/p><pre class=\"wp-block-preformatted\">export VAR=\"VALUE\"<\/pre><p>Save the changes and close the text editor. Reboot your system to apply the settings to the bash shell.<\/p><p>If you run the <strong>printenv <\/strong>command, the new configuration should show in your Linux environment variables list.<\/p><h3 class=\"wp-block-heading\">How to Delete Environment Variables in Linux<\/h3><p>Use the <strong>unset <\/strong>command to remove an environment variable in Linux. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">unset VAR<\/pre><p>Replace <strong>VAR <\/strong>with the variable you want to delete. Remember that the removal only applies temporarily, meaning the key-value pairs will return after the system reboots.<\/p><p>To permanently remove an environment variable, open your <strong>~\/.bashrc <\/strong>or<strong> \/etc\/environment <\/strong>configuration file using a text editor. Remove the corresponding line and save the changes.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/A-hash-symbol-disabling-an-environment-variable-in-the-environment-configuration-file.png\"><img decoding=\"async\" width=\"1024\" height=\"98\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/12\/A-hash-symbol-disabling-an-environment-variable-in-the-environment-configuration-file-1024x98.png\" alt=\"A hash symbol disabling an environment variable in the environment configuration file\" class=\"wp-image-100331\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/A-hash-symbol-disabling-an-environment-variable-in-the-environment-configuration-file.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/A-hash-symbol-disabling-an-environment-variable-in-the-environment-configuration-file-300x29.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/A-hash-symbol-disabling-an-environment-variable-in-the-environment-configuration-file-150x14.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/12\/A-hash-symbol-disabling-an-environment-variable-in-the-environment-configuration-file-768x74.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Instead of deleting, we recommend adding a hash symbol (<strong>#<\/strong>) at the beginning of the string to deactivate it. You can quickly reactivate the environment variable by removing the character when the change causes an error.<\/p><h2 class=\"wp-block-heading\" id=\"h-linux-environment-variables-in-development\">Linux Environment Variables in Development<\/h2><p>Environment variables let you easily set up different areas than the code bases to store sensitive information for your applications. It helps improve security since administrators can limit their access to specific users.<\/p><p>In addition, using environment variables in shell scripts helps simplify the automatic deployment process. Instead of manually writing information like API URLs in each <strong>.sh <\/strong>file, simply export them to apply the value.<\/p><p>Moreover, it simplifies application management and development since you don&rsquo;t need to modify the code base frequently. For example, we set two different API URLs as environment variables:<\/p><pre class=\"wp-block-preformatted\">export PYAPP_ENV=\"TEST\"\n\nexport PYAPP_TEST_API=\"https:\/\/api.domain.tld\/\"\n\nexport PYAPP_DEPLOY_API=\"https:\/\/api2.domain.tld\/\"<\/pre><p>We call the <strong>PYAPP_ENV <\/strong>environmental variable into the application&rsquo;s code base. During the deployment, we can change its value temporarily into <strong>DEPLOY<\/strong>, applying the <strong>PYAPP_DEPLOY_API<\/strong>.<\/p><p>Having several variables saves time, as you don&rsquo;t need to update them individually when moving the application to another development stage.<\/p><h2 class=\"wp-block-heading\" id=\"h-linux-environment-variables-best-practices\">Linux Environment Variables Best Practices<\/h2><p>In this section, we will explore the best Linux environment variable practices to help you manage your server easily and securely.<\/p><p><strong>Use Kodee for Easy Environment Variables Management<\/strong><\/p><p>All Hostinger <a href=\"\/uk\/vps-hosting\">VPS hosting plans<\/a> include <strong>Kodee<\/strong>, an AI assistant that lets you quickly generate commands or instructions using simple prompts.<\/p><p>To access the tool, open <strong>hPanel <\/strong>and click <strong>VPS <\/strong>on the top menu. Select the corresponding server and click <strong>Kodee <\/strong>from the sidebar.<\/p><p>It will write the configurations for setting Linux environment variables, which you can quickly copy-paste to the Browser terminal.<\/p><p>For example, ask <strong>Kodee<\/strong>, &ldquo;I need to generate a script to set environment variables for my project. I need three variables: <strong>&lsquo;API_KEY<\/strong>&rsquo; with &lsquo;<strong>123<\/strong>&rsquo; value, &lsquo;<strong>DB_URL<\/strong>&rsquo; with &lsquo;<strong>https:\/\/example.tld\/database<\/strong>&rsquo; value, and &lsquo;<strong>LOG_LEVEL<\/strong>&rsquo; with &lsquo;<strong>debug<\/strong>&rsquo; value. Write the Bash script for me.&rdquo;<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/10\/set-environment-variables-bash-script-generated-by-kodee.png\"><img decoding=\"async\" width=\"1024\" height=\"495\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/10\/set-environment-variables-bash-script-generated-by-kodee-1024x495.png\" alt=\"Kodee generates script for setting up environment variables\" class=\"wp-image-117125\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/set-environment-variables-bash-script-generated-by-kodee-1024x495.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/set-environment-variables-bash-script-generated-by-kodee-300x145.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/set-environment-variables-bash-script-generated-by-kodee-150x73.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/set-environment-variables-bash-script-generated-by-kodee-768x372.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/set-environment-variables-bash-script-generated-by-kodee-1536x743.png 1536w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2024\/10\/set-environment-variables-bash-script-generated-by-kodee-2048x991.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>You can also use the tool for Linux variable troubleshooting. For instance, ask <strong>Kodee<\/strong>, &ldquo;I need to change the global environment variables set for all users. I&rsquo;ve added the new file to <strong>\/etc\/environment<\/strong>,<strong> <\/strong>but it doesn&rsquo;t work. What are the possible causes and their solutions?&rdquo;<\/p><p>However, we recommend double-checking the generated script or configuration in the development area. As with any AI tool, there might be some inaccurate information.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/uk\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p><strong>Use Descriptive Environment Variable Names<\/strong><\/p><p>Use a descriptive environment variable name to simplify identification and organization. Avoid vague phrases like <strong>VAR1 <\/strong>or <strong>DATABASE_VAR, <\/strong>as they may complicate management tasks.<\/p><p>You might be unable to identify these variables and their functions, especially if you have multiple environments. In addition, changing the wrong settings can affect your system functionality.<\/p><p>We recommend using a name suggesting the variable&rsquo;s purpose. For example, if it determines the default file storage directory path, use names like <strong>LOG_PATH <\/strong>or<strong> EXECUTION_PATH<\/strong> instead of <strong>PATH<\/strong> only.<\/p><p><strong>Avoid Variable Scope<\/strong><\/p><p>Setting the right scope of environment variables in Linux is crucial to ensure system functionality and security. For instance, use a shell variable for temporary tasks, like application deployment target directory and a remote server address.<\/p><p>Meanwhile, set environment variables to define settings multiple applications use, like the HTTP proxy or locale settings.<\/p><p>Setting the wrong variable scope might override the configuration, resulting in an error. Creating a shell variable with the same name as a global one confuses your system since it doesn&rsquo;t know which one to use.<\/p><p>To avoid issues, set shell variables temporarily so they won&rsquo;t overlap when used in another session. Also, follow the Linux variable naming conventions to enable the system to distinguish between the global and local ones.<\/p><p>Use all lowercase letters to write a shell variable and uppercase for the global one. For example, use the <strong>PATH<\/strong> variable for system-wide tasks and <strong>path_deploy_pyapp <\/strong>for temporary use.<\/p><p><strong>Regularly Review and Audit Environment Variables<\/strong><\/p><p>Regularly auditing Linux variables lets you quickly catch outdated configurations that can cause server malfunctions. Updating ensures they adhere to the latest software and system configurations to help avoid incompatibility issues.<\/p><p>Moreover, they might contain sensitive information like deprecated API keys or credentials, compromising your VPS safety. Regular audits help improve environment variable security, preventing unauthorized parties from accessing them.<\/p><p>To simplify the audit process, create configuration documentation. The simplest way to do so is by exporting variables in Linux using the operator like the following:<\/p><pre class=\"wp-block-preformatted\">set &gt; user_shell_ddmmyyyy.txt<\/pre><p>In addition, the command also lets you create Linux environment variable backups for simpler restoration.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Environment variables in Linux are key-value pairs determining your system&rsquo;s shell and process behavior. They let server administrators easily distinguish and share configurations between users or applications.<\/p><p>To print all environment variables, enter the <strong>printenv <\/strong>or <strong>env <\/strong>command in Terminal. Meanwhile, run the <strong>echo $var <\/strong>utility or use <strong>printenv var <\/strong>to check a particular key-value pair.<\/p><p>Use the <strong>export <\/strong>command to set new environment variables temporarily. To create a permanent one, add the key-value pairs in the <strong>~\/.bashrc<\/strong> or<strong> \/etc\/environment <\/strong>configuration files using a text editor like <strong>nano<\/strong>.&amp;nbs<\/p><p>Add a hash symbol (<strong>#<\/strong>) at the beginning of the key-value pair in those files to disable the corresponding environment variables. Alternatively, use the <strong>unset<\/strong> command to remove them temporarily.<\/p><p>When configuring environment variables in Linux, leverage Hostinger&rsquo;s AI assistant, <strong>Kodee<\/strong>, to simplify the task. In addition, use descriptive names, avoid variable scope, and regularly review your configuration to ensure optimal server performance and security.<\/p><h2 class=\"wp-block-heading\" id=\"h-linux-environment-variables-faq\">Linux Environment Variables FAQ<\/h2><p>In this section, we will answer several common questions about Linux environment variables to help you understand them better.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1703064290477\"><h3 class=\"schema-faq-question\">How Do I Find Linux Environment Variables?<\/h3> <p class=\"schema-faq-answer\">To list current environment variables set on your system, use the <strong>printenv <\/strong>or <strong>env <\/strong>command. Pipe the <strong>less <\/strong>flag to print a shorter output.<br>Use <strong>echo $var<\/strong> or <strong>printenv var<\/strong> command to check a specific one. Meanwhile, run <strong>set <\/strong>to check both environment and shell variables.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1703064311217\"><h3 class=\"schema-faq-question\">What Are the Types of Environment Variables in Linux?<\/h3> <p class=\"schema-faq-answer\">There are <strong>local<\/strong> and <strong>global<\/strong> environment variables. Local or shell variables apply to the current session in which they are defined and reset upon system reboots.<br>Global environmental variables have two types. You can use the system-wide configuration using any account, while the user-specific variable applies to one user.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1703064328075\"><h3 class=\"schema-faq-question\">What Is the Difference Between Local and Global Linux Environment Variables?<\/h3> <p class=\"schema-faq-answer\">Local variables apply only to a specific process or the current shell instance in which they are defined. They are inaccessible to other programs or scripts.<br>Meanwhile, the global ones affect all processes, subprocesses, and shells within an environment. It is accessible to all programs of the same user.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Linux environment variables are key-value pairs defining your system&rsquo;s shell session and program behavior. They let system administrators easily share and distinguish configurations between applications and users. Admins can modify them using Linux commands to tweak their server settings based on the hosted applications&rsquo; needs. Depending on the tasks, you can also set up temporary [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/uk\/tutorials\/linux-environment-variables\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Manage Linux Environment Variables in %currentyear% + Tips","rank_math_description":"Linux environment variables are values that affect programs and processes running on a server. Learn how to modify them in Linux terminal.","rank_math_focus_keyword":"linux environment variables","footnotes":""},"categories":[22644,22640],"tags":[],"class_list":["post-16071","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\/linux-environment-variables","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/variaveis-de-ambiente-linux","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/variable-d-environnement-linux","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/linux-environment-variables","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/linux-environment-variables","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/linux-environment-variables","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/variaveis-de-ambiente-linux","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/linux-environment-variables","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/linux-environment-variables","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/linux-environment-variables","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/linux-environment-variables","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/16071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/comments?post=16071"}],"version-history":[{"count":16,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/16071\/revisions"}],"predecessor-version":[{"id":127440,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/16071\/revisions\/127440"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media?parent=16071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/categories?post=16071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/tags?post=16071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}