What is export command in linux

Mastering Linux Export Command

The export command is a bash shell BUILTIN command. It tells the Linux shell to make the variables available to the child processes. In Linux, the export command is usually used to change local variables to environment variables.

We can use the export command to list all the global variables. It is the same as the env command in this case.

We can also assign a value to a variable and then make this variable available to the shell’s environment with the export command.

This means that the variable can be accessed by the sub-shell.

I will explain this with examples later.

What are variables in Linux?

The Bash shell uses a feature called variables to store information about the shell session.

There are two variable types in the Bash shell:

  • Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.
  • Environment Variables − An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.

Environment variables are visible from the shell session and from any spawned child sub shells. Local variables are available only in the shell that creates them.

The export command is used to make local variables to environment variables.

What is shell and sub shell in Linux?

In the Linux operating system, the shell is the software that provides a command-line interface (CLI) for interacting with the operating system.

When you open a terminal window and type a command, the shell processes that command and then displays the results.

We can use the following command to get process id of the current shell.

A sub-shell is a new shell instance that is created and run within the context of parent shell.

Sub-shells can be useful in a number of situations, such as running a set of commands in a separate environment or creating a new environment for a script to run in.

How to create a sub shell in Linux?

We can run bash to create a sub shell.

$bash
$echo $$
884397
$ ps -ef|grep 884397
ocp 884397 884169 0 07:58 pts/0 00:00:00 bash

So we can see that process 884397 is sub shell of process 884169.

List all environment variables with export command in Linux

To view a list of all environment variables that are set in your shell, you can use the export or env command.

$ export
declare -x DBUS_SESSION_BUS_ADDRESS=»unix:path=/run/user/50291/bus»
declare -x HISTCONTROL=»ignoredups»
declare -x HISTSIZE=»1000″
declare -x HISTTIMEFORMAT=»%F %T »
declare -x HOME=»/home/ocp»
declare -x HOSTNAME=»howtouselinux.com»
declare -x LANG=»C.UTF-8″
declare -x LESSOPEN=»||/usr/bin/lesspipe.sh %s»
declare -x LOGNAME=»ocp»

Читайте также:  Create software raid linux

You can use echo command to get the value of a specific variable.

Note:The set command displays environment variables and local variables, user-defined variables, and local functions. To unset an variable, you can use the unset command followed by the name of the variable.

Set environment variables with export command in Linux

In the following example, a child shell was spawned via the bash command. The user-defined my_variable was not available in the child shell.

This is demonstrated by the blank line returned after the echo $my_variable command.

After the child shell was exited and returned to the original shell, the local variable was available.

$ my_variable=»Hello World»
$
$ bash
$ echo $my_variable

$ exit
exit
$ echo $my_variable
Hello World
$

Let’s export this local variable my_variable to environment variable. This is done by using the export command and the variable name (minus the dollar sign):

$ my_variable=»I am Global now»
$
$ export my_variable
$
$ echo $my_variable
I am Global now
$ bash
$ echo $my_variable
I am Global now
$ exit
exit
$ echo $my_variable
I am Global now
$

After defining and exporting the local variable my_variable , a child shell was started by the bash command. The child shell was able to properly display the my_variable variable’s value.

The variable kept its value, because the export command made it global.

To keep typing to a minimum, you can set the variable and export it all in one command. Using the previous example, you would type

export my_variable=»I am Global Now»

and press Enter at the command line.

Environment variables are visible from any child processes created by the parent process that sets the variable. The method used to create a global environment variable is to first create a local variable and then export it to the global environment.

Conclusion

Let me summarize what we get.

Before using the export command, the variable does not exist in the sub shell’s environment. This means that programs run within the shell will not be able to access the variable.

After using the export command to set an global variable, the variable is added to the shell’s environment and its value is set to the value specified in the export command.

This means that programs run within the shell and sub shell can access the variable and can use its value to customize their behavior.

For example, consider the following export command:

Before this command is run, the PATH variable only works in the current shell env. The shell does not know to search the /usr/local/bin directory for executables when you open a sub shell.

After this command is run, the PATH variable is set in the shell’s environment and its value includes the /usr/local/bin directory. The sub shell will search the /usr/local/bin directory for executables.

Note that the changes made by the export command are only effective within the current shell.

If you open a new shell or log out and log back in, the environment variables will be reset to their default values. To permanently set an environment variable, you will need to add the export command to your shell’s startup script (e.g., ~/.bashrc for the Bash shell).

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Читайте также:  Can you run linux programs on mac

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

Export Command in Linux Explained

The export command in Linux is used for creating environment variables. Understand how it works and how you can use export command for practical usage.

The export command in Linux is used for creating environment variables. You can use it like this:

or a shorthand like this to assign it a value immediately:

You can see the value of exported variables with the echo command:

To make the changes permanent, you should add it to the ~/.bashrc file.

That was just the quick summary. Let’s see it in details to understand it better.

Understanding how export command works

Export Command

In the example below, I declare a shell variable var and assign it a value 3. It’s a shell variable for now.

[email protected]:~$ var=3 [email protected]:~$ echo $var 3

If I exit the terminal and open a new terminal, this shell variable will disappear. If I want to use this variable in a shell script, it won’t work. Similarly, if I switch user (and thus initiating a new shell with this user), this shell variable won’t be available:

[email protected]:~$ su prakash Password: [email protected]:/home/abhishek$ echo $var 

Now, let’s go back to the previous user (and thus the previous shell where I declared the shell variable). You can see that the variable still exists here (because we didn’t terminate this shell session yet):

[email protected]:/home/abhishek$ exit exit [email protected]:~$ echo $var 3

So, now if I use the export command on the variable var here, it will become an environment variable and it will be available to all the subshells, users and shell scripts in this session.

[email protected]:~$ export var [email protected]:~$ echo $var 3 a[email protected]:~$ su prakash Password: [email protected]:/home/abhishek$ echo $var 3

You can check all the environment variables using the printenv command:

Make exported shell variables ‘permanent’ with bashrc file

But the struggle doesn’t end here. If you close the session, exit the terminal, log out or reboot your system, your environment variable will disappear again.

This is why it’s a common practice to add the export commands to the runtime configuration (rc) file of your shell.

Every shell has this rc file located in the user’s home directory which is used to determine variables and other configuration when the shell is started. As a user, you can use this rc file to customize your shell and its behavior.

If you are using bash shell, you should have a bashrc file at ~/.bashrc. You can either edit this file in a text editor like Vim or you can just append export var=3 (or whatever you are exporting) to this file.

Once done, you should use the source command to make the changes available immediately.

A good practice is to keep all the user defined environment variables at one place.

Why use export command?

One of the most common use of the export command is when you want to add something to the path so that your Linux system will find the certain command/executable file.

Читайте также:  Linux mint очистка места

For example, if you installed maven and you want to be able to run it, you should add the directory location of maven executables to the path like this:

export PATH=/opt/maven/bin:$PATH

What does it do? It adds this directory location to the path. When you try to run a command in Linux, your system searches for its executable (usually in bin directory) in the directories mentioned in the PATH variable.

[email protected]:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin [email protected]:~$ export PATH=/opt/maven/bin:$PATH [email protected]:~$ echo $PATH /opt/maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Bonus Tip: Remove a variable from exported list

Suppose you want to remove an ‘exported’ variable. You can use the negate option in this fashion:

Keep in mind that this will not reset the value of the variable. It will just turn the exported global variable into a local variable. It will continue to have the same value you had set earlier.

If you want to remove the variable from the exported list as well as remove its assigned value, use the unset option:

I hope you have a better idea of the export command in Linux now. If you have doubts, please feel free to ask in the comment section.

Источник

Export Command in Linux

Export Command in Linux

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

In this guide, we will look at the export command in Linux. Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes. Basically, a variable will be included in child process environments without affecting other environments. To get a clearer picture of what we are talking about, let’s dive in and have a look at the export command examples.

Export command in Linux without any arguments

Without any arguments, the command will generate or display all exported variables. Below is an example of the expected output.

Export Command in Linux without any arguments

Sample output

Viewing all exported variables on current shell

If you wish to view all exported variables on the current shell, use the -p flag as shown in the example

Export -p view all exported variables in current shell

Sample output

Using export with functions

Suppose you have a function and you wish to export it, how do you go about it? In this case , the -f flag is used. In this example, we are exporting the function name () . First, call the function

export a function using -f

You can also assign a value before exporting a function as shown

In the above example, the variable ‘student’ has been assigned the value ‘Divya’ To export the variable run

exporting a variable

Check the output below of the commands we have just executed Output The above can be achieved in 2 simple steps by declaring and exporting the variable in one line as shown

Export Command In A Few Steps

Output This concludes our tutorial about export command. Go ahead and give it a try and see the magic! Your feedback is most welcome.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us

Источник

Оцените статью
Adblock
detector