Linux entry advanced-how to use the export command in Linux

Insert picture description here

The Linux export command will mark which values ​​need to be passed to a group of child processes. This is a simple but useful feature provided by the bash shell. It allows the administrator to pass the configuration parameters of the environment without interrupting the current session. This is why the exported variables are only used after the terminal session is restarted. Fortunately, the export command is very simple to use and easy to master. In this guide, we will advise novices how to use export in Linux.

How to use Linux export command

You can use export in many ways. A common example is to use export to set up the user environment. Users can simply use export to specify variables and add them to their .profile file. Therefore, the environment will be configured in this way every time a user logs in.

1. Display all variables of export

When used without any parameters, the export command will display a list of all export variables in the environment. You should be able to view the name of the variable and its corresponding value.

linuxmi@linuxmi:~$ export

Insert picture description here

By using the Linux grep command and export command, you can easily find information about specific variables. The following command uses a simple example to illustrate this point.

linuxmi@linuxmi:~$ export LINUXMI=“Welcome to linuxmi.com”
linuxmi@linuxmi:~$ export | grep -i linuxmi

Insert picture description here

2. Display the export variable of the current Shell

The -p flag of export will print out a list of all exported variables in the current Linux shell. Take a look at the example below to see what this means.

linuxmi@linuxmi:~$ export -p

Insert picture description here

You can use this command to troubleshoot various configuration problems of the running shell session.

3. Export variables under Linux

The export command makes it easy to share variables across environments. You can use the export statement to set the value of the variable. The following example demonstrates this.

linuxmi@linuxmi:~$ export EDITOR=/usr/bin/gedit

This will set the path of gedit to the value of the editor variable. You can use grep to confirm this.

linuxmi@linuxmi:~$ export | grep -i EDITOR

Insert picture description here

4. Export function under Linux

Developers can use the -f option to export functions. The following example demonstrates this using a simple test function. You can use this method to write custom shell scripts.

linuxmi@linuxmi:~$ test () {echo "Test Function";}
linuxmi@linuxmi:~$ export -f test
linuxmi@linuxmi:~$ bash
Test Function
dircolors: /home/linuxmi/.dircolors: there is no such file or Directory
linuxmi@linuxmi:~$ test
Test Function

Insert picture description here

This should display the string "Test Function" in the terminal window. The bash call is used to spawn a child process for bash. Otherwise, the test function will not print the text.

5. Configure environment properties

Under the Linux operating system, you can use the export command to configure various environmental parameters. For example, if you add the following line to the .bashrc file, it will be set to the path of Snap every time the system restarts.

linuxmi@linuxmi:~echo export PATH="/snap/bin/lxd:echoexportPATH="/snap/bin/lxd:PATH" >> .bashrc

If you are not familiar with the inner workings of configuration files, don't worry. Just add a custom export at the end of the file. This way, you can always find and delete them if you want.

to sum up

The Linux export command is a useful tool for configuring environmental parameters. In addition, this command is very easy to master because this command has only a few different options. We have outlined some examples to help you better understand this tool. Hope that from now on, you can start using export to customize the environment. What do you think about this simple guide? Please leave us a message in the comment section, thank you.

Insert picture description here

Guess you like

Origin blog.csdn.net/liuxingjiaoyu/article/details/112670329