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:
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:
Consider the below snap of output:
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:
Consider the below snap of output:
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:
We are exporting a function 'name' as follows:
To export the above function, execute the command as follows:
Now, invoke the bash shell to execute the function:
To call the function, enter the function name:
Consider the below output:
let's create another function 'hello,' execute the command as follows:
To export the above function, execute the command as follows:
Consider the below output:
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:
For example, assign a value to a variable as follows:
now export it as:
we can verify the assignment by using the printenv command as follows:
Consider the below output:
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:
The above commands will not show any confirmation. Consider the below output:
Example6: Set an environment variable
To create a new variable, use the export command followed by a variable name and its value.
Syntax:
To create a new variable, 'sys,' execute the command as follows:
The echo command is used to display the variable:
To display the value of the variable, use the $ symbol before the variable name
Consider the below output:
No comments:
Post a Comment