Monday, 22 February 2021

Linux export Command

 The export command is a built-in utility of Linux Bash shell. It is used to ensure the environment variables and functions to be passed to child processes. It does not affect the existing environment variable.

Environment variables are set when we open a new shell session. At any time, if we change any variable value, the shell has no way to select that change. The export command allows us to update the current session about the changes that have been made to the exported variable. We do not need to wait to start a new shell session.

Syntax:

  1. export [-f] [-n] [name[=value] ...] or export -p  

Let's have a look at various examples of the export command:

Example1: The export command without any argument

The basic export command will display all the exported environment variables of your system. It is executed as follows:

  1. export  

Consider the below snap of output:

Linux export Command

Example2: Display all exported variable on current shell

To display all the exported environment variable of the current shell, execute the command with -p option as follows:

  1. export -p  

Consider the below snap of output:

Linux export Command

Example3: Using export with functions

To use a function with the export command, use the -f option. If we do not use this option, it will be considered as a variable, not function.

Syntax:

  1. export -f function_name  

We are exporting a function 'name' as follows:

  1. name() { echo "Javatpoint";}  

To export the above function, execute the command as follows:

  1. export -f name  

Now, invoke the bash shell to execute the function:

  1. bash  

To call the function, enter the function name:

  1. name  

Consider the below output:

Linux export Command

let's create another function 'hello,' execute the command as follows:

  1. function hello  
  2. > {  
  3. > echo hello, welcome to javatpoint  
  4. > }  

To export the above function, execute the command as follows:

  1. export -f hello  

Consider the below output:

Linux export Command

Example4: Assign a value before exporting a function or variable:

The export command allows us to assign a value before exporting a function. Consider the below command:

  1. export name[=value]  

For example, assign a value to a variable as follows:

  1. a=5  

now export it as:

  1. export a  

we can verify the assignment by using the printenv command as follows:

  1. printenv a  

Consider the below output:

Linux export Command

Example5: Set vim as default editor:

The vim editor is the most widely used text editor for the Linux systems. We can set the vim as default text editor by using the export command.

To set the vim as a default text editor, execute the following command:

  1. export EDITOR=/usr/bin/vim  
  2. export | grep EDITOR  

The above commands will not show any confirmation. Consider the below output:

Linux export Command

Example6: Set an environment variable

To create a new variable, use the export command followed by a variable name and its value.

Syntax:

  1. export NAME=VALUE  

To create a new variable, 'sys,' execute the command as follows:

  1. export  sys=50  

The echo command is used to display the variable:

  1. echo sys  

To display the value of the variable, use the $ symbol before the variable name

  1. echo $sys  

Consider the below output:

Linux export Command

No comments:

Post a Comment