Environment variables, predefined variables and location variables of Shell programming for birds

Keywords: Linux shell

environment variable

Environment variables: Variables that can be obtained by opening each Shell.
We know that by export ing, we can let the child process read the value of the variable of the parent process, so how can each process read the value of the variable?

In this case, the system has some default configuration files, which can be embedded in the configuration file.

So, what environmental variables have been brought into the system? We can view the built-in link variables in the system through the command env.

[root@lincoding ~]# env
HOSTNAME=lincoding
SHELL=/bin/bash
......
SSH_TTY=/dev/pts/0
USER=root
......
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
LANG=en_US.UTF-8
SHLVL=1
HOME=/root
LOGNAME=root
......

Because there are too many variables in links, a part of them is omitted. These are all environment variables of the system. When a new terminal is opened, the above variables will be initialized.

You can see the value of a single environmental variable by referring to the variable, which is mainly capitalized.

[root@lincoding ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@lincoding ~]# echo $SHELL
/bin/bash

PATH system environment variable defines the search path of the command, and SHELL defines the default shell of the system as bash.

PATH environment variable

The PATH system environment variable defines the search PATH of the command, which means that the Linux commands we enter will be found in the PATH defined by the PATH variable. If it exists, the command will be executed. If it does not exist, the error reporting command does not exist.
In fact, the so-called non-existence is that no corresponding command is found in the search path.

[root@lincoding ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Assuming you want to add a custom search path, you can do this in the following way

[root@lincoding ~]# PATH=$PATH:/home
[root@lincoding ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/home

However, in the above way, the new custom path is the PATH variable, which only takes effect in the current terminal scope of action, while other processes do not.

Predefined variables

The predefined variables are $?, $, $0

  • The $? Represents the status code of the previous command, 0 represents normal, and non-zero represents error.
[root@lincoding home]# cd /home/
[root@lincoding home]# echo $?
0
[root@lincoding home]# cd /file
-bash: cd: /file: No such file or directory
[root@lincoding home]# echo $?
1
  • $View the pid of the current process
[root@lincoding home]# echo $$
18136
  • $0 denotes the name of the current process
[root@lincoding home]# echo $0
-bash

These three predefined variables are used in the Shell script, which is as follows:

#!/bin/bash

# PID
echo $$

echo $0

Implementation effect:

[root@lincoding home]# ./test.sh
702
./test.sh
[root@lincoding home]# . test.sh
18136
-bash
[root@lincoding home]# source test.sh
18136
-bash

Depending on how it is executed, the name of $0 varies.

Position parameter

Location parameters are used to read the values of the parameters passed in when executing the Shell script as follows:

  • $1 parameter 1
  • $2 parameter two
  • $3 parameter three

By analogy, when the parameter is more than 10, we need to pay attention to it. We need to use and .

For example, the following Shell script:

#!/bin/bash

# $1 $2 $3 ... $9 ${10}
echo $1
echo $2
echo $3

Implementation results:

[root@lincoding home]# ./test.sh a b c
a
b
c

Here's how to get the total number of parameters from the incoming Shell script using $#, for example:

#!/bin/bash

echo $#

Implementation results:

[root@lincoding home]# ./test.sh
0
[root@lincoding home]# ./test.sh a
1
[root@lincoding home]# ./test.sh a b
2
[root@lincoding home]# ./test.sh a 123 b
3

Summary

This section mainly introduces the particularity of environment variables. PATH environment variables define the search path of commands. If you want to know other environment variables, you can use env command to view them.

The predefined variables are the predefined variables of the system, which are $?, $and $0, respectively. And you can also get the value of the Shell's incoming parameters by using the location variable $1 $2 $3.

Posted by jrottman on Thu, 03 Oct 2019 07:33:44 -0700