environment variables – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Wed, 14 Sep 2022 03:46:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How to Set JAVA_HOME environment variable on macOS https://tecadmin.net/how-to-set-java_home-environment-variable-on-macos/ https://tecadmin.net/how-to-set-java_home-environment-variable-on-macos/#respond Tue, 13 Sep 2022 12:26:53 +0000 https://tecadmin.net/?p=31670 This tutorial will help you to set the JAVA_HOME environment variable on the latest older macOS operating systems. The /usr/libexec/java_home is the command line utility that returns the Path of the Java home directory from the current user’s settings. You can use this utility to set up the JAVA_HOME environment variable on your macOS. In [...]

The post How to Set JAVA_HOME environment variable on macOS appeared first on TecAdmin.

]]>
This tutorial will help you to set the JAVA_HOME environment variable on the latest older macOS operating systems. The /usr/libexec/java_home is the command line utility that returns the Path of the Java home directory from the current user’s settings. You can use this utility to set up the JAVA_HOME environment variable on your macOS.

In an Operating System (OS) JAVA_HOME environment variable must be set to point to the directory containing the JVM. In this tutorial, we will help you to correctly set the JAVA_HOME environment variable on macOS.

Check Pre-Installed Java Versions

You can find details of all installed Java versions on macOS with the following command.

/usr/libexec/java_home -V 
Ouput:
Matching Java Virtual Machines (5): 18.0.1, x86_64: "OpenJDK 18.0.1" /Library/Java/JavaVirtualMachines/temurin-18.jdk/Contents/Home 16.0.1, x86_64: "AdoptOpenJDK 16" /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home 15.0.1, x86_64: "AdoptOpenJDK 15" /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home 11.0.9.1, x86_64: "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home 1.8.0_275, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home /Library/Java/JavaVirtualMachines/temurin-18.jdk/Contents/Home

Set JAVA_HOME on macOS 10.15 and newer

The macOS Catalina (version 10.15) and the newer macOS versions usee the Zsh as the default shell. Zsh executes ~/.zshrc script during a new session starts. You can add your code in this file to the JAVA_HOME environment variable. Once the user is logged or opens a new shell the variable will automatically be set by the script.

Use one of the below commands to set JAVA_HOME as per the required Java version:

  • Use default Java version:
    echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc 
    
  • Set the specific Java version by specifying it as:
    echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.8)" >> ~/.zshrc 
    

    To use a different Java version, simply replace 1.8 with the version you required like 11, 15, 17, or 18.

Set JAVA_HOME on macOS 10.14 Mojave and older

The macOS Mojave (version 10.14) and the previous macOS versions uses the Bash as the default shell. Bash runs ~/.bash_profile script everytime it started. You can easily set the JAVA_HOME environment variable using this file.

  • Set the default Java version:
    echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
    
  • Set the specific Java version:
    echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.8)" >> ~/.bash_profile
    

    To use a different Java version, simply replace 1.8 with the version you required like 11, 15, 17, or 18.

Conclusion

The JAVA_HOME is an environment variable that points to the file system location where the JDK or JRE is installed. Many Java application uses this variable to find the location of Java installed on the system.

The post How to Set JAVA_HOME environment variable on macOS appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-set-java_home-environment-variable-on-macos/feed/ 0
How to Correctly Set the $PATH variable in Bash https://tecadmin.net/setting-up-path-environment-variable-in-bash/ https://tecadmin.net/setting-up-path-environment-variable-in-bash/#respond Wed, 03 Aug 2022 12:19:38 +0000 https://tecadmin.net/?p=30798 Bash is an acronym of Bourne-Again Shell, which is the successor of Bourne Shell distributed with most of the Linux and GNU operating systems. It comes with multiple advanced features from the previous version. The PATH is an environment variable that stores the directories path containing the executable files. How to Set PATH Variable? Whenever [...]

The post How to Correctly Set the $PATH variable in Bash appeared first on TecAdmin.

]]>
Bash is an acronym of Bourne-Again Shell, which is the successor of Bourne Shell distributed with most of the Linux and GNU operating systems. It comes with multiple advanced features from the previous version.

The PATH is an environment variable that stores the directories path containing the executable files.

How to Set PATH Variable?

Whenever you need to add a new executable in the PATH variable, you can either add it to the start of another directory or at the end of other directories.

The system checks for any executable under directories set in PATH from start to end. If the required binary is found in a directory, it will ignore the rest. In case, you have added an executable at the end, the system may ignore that if an executable is found with the same name in previously configured directories.

  • Setting up after other executables:
    export PATH=${PATH}:/opt/maven/bin
  • Setting up before other executables:
    export PATH=/opt/maven/bin:${PATH}
Info: When using the /etc/environment file, just set the path without export command, like:
PATH=/opt/maven/bin:${PATH}

Where to Set the PATH Variable?

There are multiple scripts available in the Linux system where you can set the PATH environment variable. These scripts are executed in predefined conditions. Like, some scripts are executed during system startup, and some of them are executed at user login or logout.

Below is the list of scripts that are executed under different conditions. You can choose one of them to set the PATH variable.

  • System-wide Configuration:
    • /etc/bashrc: This script is invoked at system startup. Setting the PATH environment to this file will be available for all users.
    • /etc/bash.bashrc: This script is invoked for the interactive and login shells.
    • /etc/profile: This script is invoked with login shells only.
    • /etc/profile.d/*.sh: All the script with “.sh” extension are invoked by /etc/profile script.
    • /etc/environment: This is the first file that operating system uses at login. This is specifically used for setting up environment variables. This file doesn’t require to use “export” keyword.

  • User level configuration:
    • $HOME/.bashrc: This file is invoked for non-login shell.
    • $HOME/.profile: This file is invoked for the login shell. Also, this file invokes the ~/.bashrc script.

Conclusion

The PATH environment variable is a necessary part of the Linux and other GNU-based operating systems. You must be careful before setting up the PATH variable.

The post How to Correctly Set the $PATH variable in Bash appeared first on TecAdmin.

]]>
https://tecadmin.net/setting-up-path-environment-variable-in-bash/feed/ 0
Setting Up Environment Variables on Ubuntu https://tecadmin.net/setting-up-environment-variables-on-ubuntu/ https://tecadmin.net/setting-up-environment-variables-on-ubuntu/#respond Sat, 28 May 2022 17:42:09 +0000 https://tecadmin.net/?p=29645 An environment variable contains a value, that is used to change the behaviors of the processes at run time. Similar to the other operating systems, we can also set the environment variables on a Ubuntu system. You can set the environment variables in 3 ways: Using the export command Using /etc/environment file Adding shell script [...]

The post Setting Up Environment Variables on Ubuntu appeared first on TecAdmin.

]]>
An environment variable contains a value, that is used to change the behaviors of the processes at run time. Similar to the other operating systems, we can also set the environment variables on a Ubuntu system.

You can set the environment variables in 3 ways:

  1. Using the export command
  2. Using /etc/environment file
  3. Adding shell script under /etc/profile.d/ directory

Now we will discuss the above methods to set environment variables on Ubuntu systems one by one.

1. Using the export command

You can use the export command on the terminal to set the environment variables temporarily. That variable will be accessible on the same terminal only. Once you close the terminal the variable will be destroyed.

To set the environment variable, run:

export MY_ENV=value 

To print the MY_ENV environment variable, type:

echo $MY_ENV

2. Using /etc/enviroment file

The /etc/environment is a system-wide configuration file used for setting the environment variables. It is not a shell script, it consists of the assignment expressions, that set the environment variables one per line.

sudo vim /etc/environment
/etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 MY_HOME="/home/tecadmin"

You can set multiple environment variables in this file. Each environment variable must be in a separate line.

During the system reboot, the environment variable written in this file will automatically be assigned and accessible system-wide.

3. Using /etc/profile.d/*.sh files

You can also create a shell script under the /etc/profile.d directory. During the user login /etc/profile script is executed. Tha also executed all the shell scripts (files with .sh extension) under /etc/profile.d directory.

Let’s create a shell script /etc/profile.d/custom-env.sh and set the environment variables using export command.

sudo vim /etc/profile.d/custom-env.sh 

Set the environment variables like:

/etc/profile.d/custom-env.sh
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export JRE_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export MY_ENV="value1"

The next time the user logged in will automatically set the environment variables. You can print the value of the environment variable using the echo command.

echo $MY_ENV 

value1
Setting up environment variable in Ubuntu
Print environment variable value

Conclusion

This tutorial provides you with the details of setting up the environment variables on the Ubuntu system. These environment variables are very helpful to change the run time behaviors of processes.

I hope this tutorial helped you with the basic understanding of creating environment variables on Ubuntu and Debian systems. Please provide your valuable suggestions in the comments and do share this article with the social platform.

The post Setting Up Environment Variables on Ubuntu appeared first on TecAdmin.

]]>
https://tecadmin.net/setting-up-environment-variables-on-ubuntu/feed/ 0
How To Set/Create Environment and Shell variables in Linux https://tecadmin.net/how-to-set-create-environment-and-shell-variables-in-linux/ https://tecadmin.net/how-to-set-create-environment-and-shell-variables-in-linux/#respond Sat, 10 Jul 2021 07:00:18 +0000 https://tecadmin.net/?p=26586 Shell, the command interpreter of an OS, processes the commands entered into the command line and widely used in Linux. A variable is like a pointer to a particular data and holds a particular value that could be anything. A user can create, delete or assign variables with the help of Shell. When a Shell [...]

The post How To Set/Create Environment and Shell variables in Linux appeared first on TecAdmin.

]]>
Shell, the command interpreter of an OS, processes the commands entered into the command line and widely used in Linux. A variable is like a pointer to a particular data and holds a particular value that could be anything. A user can create, delete or assign variables with the help of Shell.

When a Shell is running there are three types of variables present:

  1. Local Variables
  2. Shell variables
  3. Environment variables

In this tutorial, we will see how you can set Environment and Shell variables in Linux.

What are Shell Variables?

The Shell variables are specific to the current shell and used by the Shell to function correctly. These variables are temporary, and to make them permanent you can export them as environment variables.

Some of the common shell variables are:

  • UID: Current logged in user’s ID
  • HOSTNAME: The hostname of the computer at a particular time
  • BASH_VERSINFO: Machine-readable form of bash version
  • BASH_VERSION: Human-readable output of bash version
  • DIRSTACK: Stack of directories available with ‘popd’ and ‘pushd’ command
  • SHELLOPTS: Shell options can be set

What are Environment Variables?

Environment Variables are system-wide available variables that are available to any program or child process of the shell. Also, the Shell Script defines the environment variables that are needed to run the program.

Some of the common Environment variables are:

  • MAIL: Mail directory of the user or the path to user’s mailbox
  • TEMP: Temporary files’ director location
  • PWD: Current working directory
  • OLDPWD: The previous working directory
  • USER: The current user logged in
  • LANG: The current language
  • LOGNAME: User Name
  • HOME: The home directory of the current user
  • _: Recent previous command executed
  • SHELL: The current shell
  • EDITOR: The default text editor of Linux
  • HOSTNAME: The hostname of the device on the network

Here are some of the commands that will help you in setting Shell and Environment variables:

  • env: You can use a custom environment to run another program without modifying the existing environment.
  • printenv: This will print all the environment variables of your system.
  • set: Used to set the environment and shell variables.
  • unset: Used to delete the environment and shell variables.
  • exports: Let’s you export shell variables into environment variables.

How to print Shell and Environment variables?

Shell keeps a track of all Environment and Shell variables. And to access that we can use these two commands:

  • printenv: to print all the Environment variables.
  • set: to print Shell variables.

How to set Environment and Shell variables in Linux?

So now we know that what Environment and Shell variables are. So let’s learn how to create and set these variables in Linux. To clearly understand the difference between Environment and Shell variables practically. We will first set a Shell variable and then the Environment variable.

Creating the Shell variable

  • To set a Shell variable run this command in your Shell. This will create a Shell variable that will be available in your current session.
    TESTVAR='Hello!' 
    
  • We can also check our variable with the grep command.
    set | grep TESTVAR
    

    You will see an output like this.

    TESTVAR='Hello!'
    
  • You can also see the value of a shell variable with the following command.
    echo $TESTVAR
    
  • As it is a Shell variable, so it will not be available for other applications or child processes. You can even verify that it is not an Environment variable.
    printenv | grep TESTVAR
    

    There will be no output. It means it is not an Environment variable.

Creating the Environment Variables

  • Now, let’s export the Shell variable into an Environment variable. Use the following command to do that.
    export TESTVAR 
    
  • This will turn our Shell variable into an Environment variable and to verify that run the following command.
    printenv | grep TESTVAR 
    

    This time you will see an output like this.

    TESTVAR='Hello!'
    
  • Now we know that how we can convert a Shell variable into an Environment variable but how to create an Environment variable directly. You can set an Environment variable in a single step with this command.
    export NEWVAR="Hello Env" 
    
  • It will be directly exported as an Environment variable and you can verify that.
    printenv | grep NEWVAR 
    

    You will see an output like this.

    NEWWAR=Hello Env 
    

This way you can set Shell and Environment in Linux.

Unsetting variables

Now you know that how to set variables. Let’s learn about unsetting them.

  • You can change an Environment variable into a Shell variable again with this command.
    export -n TESTVAR 
    
  • It will remain a Shell variable but not an Environment variable. Let’s verify that.
    printenv | grep TESTVAR 
    
  • There will be no output but if we check it for the Shell variable,
    set | grep TESTVAR 
    

    you will see the output.

    TESTVAR='Hello!'
    
  • And if you want to completely unset that then use this command.
    unset TESTVAR 
    

Conclusion

Environment and Shell variables can be very useful for you if you often work with Shell and learning how to set and unset these variables will be good from a long-term perspective.

The post How To Set/Create Environment and Shell variables in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-set-create-environment-and-shell-variables-in-linux/feed/ 0
How To Use Environment Variables in Node.js https://tecadmin.net/how-to-use-environment-variables-in-nodejs/ https://tecadmin.net/how-to-use-environment-variables-in-nodejs/#respond Sat, 03 Jul 2021 03:31:31 +0000 https://tecadmin.net/?p=26467 Instead of defining the variables directly, you can use Environmental variables in node.js. You can use the Environmental variable anytime when you want to change the value depending on the environment. There are various use cases here. If you start using the environmental variable, you won’t face issues where the client says “It doesn’t work [...]

The post How To Use Environment Variables in Node.js appeared first on TecAdmin.

]]>
Instead of defining the variables directly, you can use Environmental variables in node.js. You can use the Environmental variable anytime when you want to change the value depending on the environment. There are various use cases here.

If you start using the environmental variable, you won’t face issues where the client says “It doesn’t work in my system”. This guide will cover all the things you should know about defining the Environmental variables and how you can use them in node.js. It has a lot to do with the .env file a well as with server.js. So, let’s begin the steps and see the steps.

Please read the code as you will have to enter some of the information such as key, port number, etc. We have mentioned it in the code itself and if possible, we have also added the comments.

Step 1 – Preparation

At first, you will have to set the port. Here is the code you can use.

// server.js
const port = process.env.PORT;
console.log(`Your port is ${port}`);

This code will go in the server.js file. You will have to create a file with the same name.
Thereafter, you also need to create the .env file. As of now, you don’t need to add anything to the env file.
You can do it by the command line. Just enter the name of the variable and then you can use the “=” sign for the value.

Here is an example of the same.

PORT=8626 node server.js 

Define Port at Runtime Environment in Nodejs

In the same way, if you want to pass two variables, you can see the below given example.

PORT=8626 NODE_ENV=development node server.js 

Now, this was how you can do the procedure using the command line. People don’t use the command line because there could be typing mistakes and it’s too complicated.

We will now focus on the .env file and do things from there.

You can add the following code in the env file.

NODE_ENV=development
PORT=8626
# Set your database/API connection information here
API_KEY=**************************
API_URL=**************************

Step 2 – Focus on .gitingore

Don’t forget about the .gitignore. You need to ensure that you have added the env file before you commit the changes.

One can simply press CMD + Shift + F and it will open the command prompt. You can enter “ignore” and then select the file you want to add.

Step 3 – Create package.json

You will first have to create a package.json. Run the code and it will then create a file for you with all the basic details.

npminit -y 

This file is needed to read the env file.

Step 4 – Install and edit env file

You can use the below code to install the env file.

npm install dotenv 

The above-mentioned line will fill the entry in the package.json that we created in the above step.

You can then replace the content of server.js with the following code.

// server.js
console.log(`Your port is ${process.env.PORT}`); // undefined
constdotenv = require('dotenv');
dotenv.config();
console.log(`Your port is ${process.env.PORT}`); // 8626

Step 5 – Find the Variables

You can run the code and find the variables with ease. We will again use the command line here. We will get the list from server.js.

node server.js 

You can define the variable now. To do that, you will have to create the variable in your .env file.

process.env.YOUR_ENV_VAR_GOES_HERE

It’s time to organize things manually. You can create a config.js file and add the code there.

// config.js
constdotenv = require('dotenv');
dotenv.config();
module.exports = {
endpoint: process.env.API_URL,
masterKey: process.env.API_KEY,
port: process.env.PORT
};

Once you do this, it’s time to modify the server.js file.

// server.js
const { port } = require('./config');
console.log(`Your port is ${port}`); // 8626

Define Port in .env in Nodejs

Define the constant in Config.js by adding the belw code.

const{ endpoint, masterKey, port } = require(‘./config’);

Step 6 – Reduce the Dependencies

It’s time to reduce the dependencies of the code. Nobody likes runtime dependencies. It’s not good in the long term. Luckily, there is a code through which you can remove the runtime dependencies in dotenv package.

You can load all the variables at first. This will decrease the time of execution. The next thing you can do here is to save the dependencies as dev dependencies. You can run the following line. This is where we will install dotenv and save it as dev. Here is the code for the same.

npm install dotenv --save-dev 

Thereafter, you can remove the code that has dependencies. This will also include the code from dotenv.config().

But wait, there exists a small problem. We were previously dependent on the .env file to load the variables. Now, we will preload all of them. The “- require (-r)” command will do the work.

You can also remove all the references.

node -r dotenv/config server.js 

The above-given code will be much useful in this.

As the code is preloaded, half of the work is done but there are still some of the things that need to be defined.

Step 7 – NPM Scripts

Put all the commands in the NPM script. This will make it easier to run and will give you a better loading time with fewer errors.

Here is the code you can have a look at.

scripts: {
"start_local": "node -r dotenv/config server.js"
}

Step 8 – NPM Extensions

It becomes hard to learn all the scripts. Therefore, you can use the extension. There is a great extension you can use here. It’s the npm extension. You can also use the npm script outline if you want.
This will make it easier to remember the lines.

Step 9 – Finalize

Now, it’s time to finalize all the things before you share them with the team. Check all the things before you send them to them.

Additionally, you can also use the .env.example to send the file.

# .env.example
NODE_ENV=development
PORT=8626
# DB information
API_KEY=your-core-api-key-goes-here

What we have done here is that we have created a template of the file. The template is very much useful when you are defining the environmental variables.

Final Words

To summarize, this is how you can start using environmental variables in your node.js. You can share it with your team and it will work smoothly. Whether you want to run in your system or your client’s system, there will be no issues. If you are using Docker like most of the pro developers do, the code will surely run there too. So, there will be no issues. You can easily run the code. What we recommend here is to add your logic and write a better code depending on your requirement.

The post How To Use Environment Variables in Node.js appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-use-environment-variables-in-nodejs/feed/ 0