Shell Script

4 Environment variables

트리스탄1234 2022. 8. 11. 06:11
728x90
반응형

There are two environment variables in Linux environment. One is a global environment variable and the other is a local environment variable. Global environment variables are environment variables that are displayed throughout the Linux system, and global environment variables are created in all uppercase letters.

1. How to check global environment variables and local environment variables

The command to view the global environment variable is printenv, and the default values ​​are displayed when logging in to the Linux system. Please refer to the picture below.

 

반응형

As you can see from the screen above, there are too many kinds of Global environment variables. So what if you want to see only a specific variable? Just use the 'echo' command. For example, if you want to see the HOME environment variable, run the command echo $HOME to display only the variable as shown in the figure below.

Local environment variables are only visible to the process that created them. And unfortunately there is no command that can only see local environment variables. However, if you use the set command, you can see both global and local environment variables.

2. Setting Environment Variables

Local variable runs the shell, and then the user can create the desired variable. To create a variable name = value, the value can contain both letters and numbers. However, when assigning a string to a variable, you have to use it as a 'value'. And there must not be a space between variable name=value, and when checking the variable value, you can check it by putting echo $variable name.

When a value is entered into a variable, the variable can be used in the same shell process, but cannot be used in another shell process. ​ And variables created in the child process cannot be used when moving to the parent process.

To set the global environment variable, create a local environment variable and export the environment variable to become a global environment variable. Do not use $ to check the variable after it has been exported. ​ To delete a variable, you can delete the created variable by entering unset variable name. Note that we do not use the $ sign in front of the variable after the unset command.

Another thing to note is that even if a variable is created in the child process and exported as a global variable, and the variable is deleted in the child process state, remember that the variable exists in the parent process.

3. Default Shell Environment Variables

Linux automatically creates default shell environment variables for system default settings. The table below lists important variables.

The PATH variable contains the locations of directories, each separated by a colon. The PATH variable defines the location to find the executable file of the command when executing a command in the shell.

For example, if a file called test12.sh is created in the directory called path and executed in the upper directory, the location of the file to be executed must be additionally specified in the PATH variable as follows.

PATH=$PATH:/home/user/path

A simpler way to do this is to use the single dot (.) mark. A single dot mark indicates the directory where the current prompt is located.

PATH=$PATH:.

4. How Default Environment Variables Are Set

There are three cases in which the default environment variables of the Linux system are set as follows.

1) When logging in to the Linux system: When logging in to the system, Bash Shell is started as Log in Shell, and 4 startup files are searched. The search order is as follows,

A./etc/profile: Startup file that is executed first, sh file in /etc/profile.d is executed

B.$HOME/.bash profile : The other 3 files set environment variables for each user's characteristics

C. $HOME/.bash login D. $HOME/.profile

2) When shell is executed after login (Interactive Shell): .bashrc in the user's home directory Environment variables are set using file files. This file searches the /etc/bashr file first and provides space for the user's Allias.

3) Shell Script (Non-interactive shell) executed by the system: When the system is running, refer to the BASH ENV variable to check the script file to be executed and execute it. (default is not set)

4. Variable Array: Environment variables are arrays, and multiple values ​​can be stored in environment variables. To set, list the values ​​with spaces in parentheses as shown below.

$ mytest=(one two three four five)

$ echo $mytest

one

$

If you just output a variable, you can see only the first value and not the other values. If you want to see the value at a specific location, you must give the index value of the value you want to see after the variable name as shown below. The index value starts from 0.

$ echo ${mytest[2]}

three

$

If you want to see the whole, you can write * sign after the variable name.

$ echo ${mytest[*]}

one two three four five

$

It is possible to change the value of a specific index in the array as follows.

$ mytest[2]=seven

$ echo ${mytest[*]}

one two seven four five

$

You can also clear values ​​in a specific array.

$ unset mytest[2]

$ echo ${mytest[*]}

one two four five

$

$ echo ${mytest[2]}

$ echo ${mytest[3]}

four

$

5. Using the Command Alias

Linux provides a default alias, and to view the default alias, run the alias -p command.

if you personally want to create an Alias, you can use it by entering the command below. alias Anonymous command to use = 'Actual commands and options'

After setting as above, Alias ​​works as a local local variable. In other words, if you open another terminal and use it, it will not work.

#environment

#linux

#variables

#alias

#PATH

728x90
반응형

'Shell Script' 카테고리의 다른 글

6. Basic Shell Script writing  (3) 2022.08.11
5 Linux File Permission  (2) 2022.08.11
3 Bash Shell Command  (8) 2022.08.10
2.Bash Shell Basic Command  (6) 2022.08.10
1 Linux command Line  (3) 2022.08.10