Birds Beginner Shell Programming (viii) environment variables, predefined variables and variable position

Environment Variables

Environment variables: Each Shell Open can get to the variables.
We know that by exportthe way is opened so that the child can read the value of the parent process variable, then how to make each process variable values can be read into it?

Here we are, there are some system default configuration file, the embedded variable to the configuration file on it.

Well, the system already comes with what environment variables? We can pass envthis command to check the system built-in link variable.

[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 too many links variable, omitted part. These are system environment variables, the new terminal is opened, the above variables are initialized completed.

By reference to a variable can view the value of a single environment variables, environment variables are capitalized based.

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

PATHSystem environment variables are defined in the search path of the command, SHELLit is to define the system default is Shell bash.

PATH environment variable

PATHSystem environment variables are defined in the search path of the command, meaning that we enter Linux commands, will in PATHseeking the path variable defined, the command is executed if there is, if there is an error command does not exist.
In fact, the so-called does not exist, the corresponding command is not found in the search path.

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

Suppose you want to increase the custom search path, by the following manner

[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, the above way, the new custom path PATH variable is only effective on the current terminal scope, other processes are not in force.


Predefined variables

Predefined variables $?, $$, $ 0 three

  • $? Represents the status code on a command execution 0 = normal, representatives of non-zero 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 current process pid
[root@lincoding home]# echo $$
18136
  • $ 0 represents the name of the current process
[root@lincoding home]# echo $0
-bash

Shell scripts using the three pre-defined variables, the script is as follows:

#!/bin/bash

# PID
echo $$

echo $0

Implementation of the results:

[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 the implementation, the names $ 0 is also different.


Positional parameters

Location parameter for reading the incoming Shell scripts execution parameter values, the following form:

  • $ 1 a parameter
  • $ 2 Parameter Two
  • $ 3 Parameters three

So, when the parameter is 10 or more when the need to look at, {10} $ need, {11} $ embodiment.

For example, the following Shell script:

#!/bin/bash

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

Results of the:

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

Here is what you get passed the total number of parameters Shell script can be used $#, the following examples:

#!/bin/bash

echo $#

Results of the:

[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 describes the particularity, the environment variable PATHenvironment variable is defined in the search path of the command, if you want to know the other environment variables, you can use the envcommand.

Predefined system variables are already pre-defined variables, namely $?, $$, $ 0. And also by the location variable $1 $2 $3... to get the value of the parameter passed in the Shell.

Guess you like

Origin www.cnblogs.com/xiaolincoding/p/11616315.html