Environment variables are not hard to understand

Environment variables refer to the variables used in the operating system environment.

We execute the following command line within bash:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Copy the code

Ubuntu terminal

In this case, PATH is the most famous environment variable, indicating where the system searches for executable commands when executing script commands. These locations are separated by:.

Other common ones are JAVA_HOME and the like.

Use export to modify environment variables

$ export PATH=/home/mk/cli
$ echo $PATH
/home/mk/cli
Copy the code

Then try the following node

$node command'node'Can be in'/snap/bin/node'Because /snap/bin is not in the PATH environment variable, the command cannot be found. Node: command not foundCopy the code

However, this operation is session-level. Closing this operation will not affect other terminal Windows.

Persist custom environment variables using Bashrc

There is an assumption that you are using bash. If the Mac address is ZSH, run ~/.zshrc.

Add a summation command to ~/.bashrc:

Create or append content to a file
$ echo "PATH=$PATH:/home/mk/cli" >> ~/.bashrc
Make it work
$ source ~/.bashrc
# Check the PATH variable
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/mk/cli
Copy the code

You can see that the CLI directory is already in the PATH. This is user-level configuration and does not affect other users.

If you open a new terminal without using the source command, you will see the same effect.

Write your own tools in the CLI

After the previous operation, /home/mk.cli has been registered globally, and commands in it can be executed globally.

Start by writing a shell script

# Write the script content to
$ echo "echo abc" >> abc.sh
Create a soft link
$ ln -s abc.sh abc
Copy the code

Try to run ABC

$ABC bash: /home/mk.cli/abc: the permission is insufficientCopy the code

We created the CLI in the user directory, so we don’t have permissions for bash. Change to 777:

$ chmod -R 777 ~/cli
$ abc
abc
Copy the code

Write executable JavaScript scripts

Create the xyz.js file in the CLI and write the following:

#! /usr/bin/env node
console.log('xyz')
Copy the code

Then create a soft link as before:

$ ln -s xyz.js xyz
$ xyz
xyz
Copy the code

The operation succeeds. Pay attention to the permissions of CLI directories and files.

The above process is the same as NPM install -g:

  • In the globalnode_modulesInstalled in thenpmpackage
  • According to thepackage.jsonthebinSet in the/usr/local/binTo create a soft link

/usr/local/bin is in the PATH variable, so you don’t need to enter the PATH prefix.

$ ls -l /usr/local/bin Total usage 0 create-react-app ->.. /lib/node_modules/create-react-app/index.js webpack -> .. /lib/node_modules/webpack/bin/webpack.js webpack-cli -> .. /lib/node_modules/webpack-cli/bin/cli.jsCopy the code

Manually install multiple versions of Node

Take a look at the current Node version

V14.17.1 $node - vCopy the code

Download the latest V16 version from the official website

Nodejs.org/dist/v16.4….

After decompressing, mv to /usr/locat/etc.

The command path for node16 is

/usr/localThe/etc/node - v16.4.0 - Linux - x64 / bin/node/usr /localThe/etc/node - v16.4.0 - Linux - x64 / bin/NPM/usr /localThe/etc/node - v16.4.0 - Linux - x64 / bin/NPXCopy the code

verify

$ /usr/local/etc/node-v16.4.0-linux-x64/bin/node -v v16.4.0 $/usr/localThe/etc/node - v16.4.0 - Linux - x64 / bin/NPM -v v7.18.0Copy the code

Create a soft link in /usr/local/bin:

sudo ln -s /usr/localThe/etc/node - v16.4.0 - Linux - x64 / bin/node/usr /local/bin/node
sudo ln -s /usr/localThe/etc/node - v16.4.0 - Linux - x64 / bin/NPM/usr /local/bin/npm
sudo ln -s /usr/localThe/etc/node - v16.4.0 - Linux - x64 / bin/NPX/usr /local/bin/npx
Copy the code

Open a new terminal window and verify:

$node -v v16.4.0 $NPM -v 7.18.1 $NPX -v 7.18.1Copy the code

Verification passed. The above.