Table of contents
1.
Introduction
2.
Export Command in Linux Without Any Arguments
2.1.
export
2.1.1.
Viewing All Exported Variables on Current Shell
3.
Using Export with Functions
3.1.
export -f greet
3.2.
export -f greet
4.
Frequently Asked Questions
4.1.
What does the export command do in Linux?
4.2.
How can I see all the variables that are exported?
4.3.
Can I unexport a variable or function?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux Export Command

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Linux is a powerhouse for developers & system admins, offering a wide range of commands to streamline work. One such gem is the export command, a utility that might seem simple at first glance but is crucial for managing environment variables in your shell. 

Linux Export Command

By mastering this command, you'll be able to control how your programs run in the Linux environment. We'll explore the basics of the export command, how to use it without arguments, view all exported variables, apply it with functions, & understand its options. Ready to enhance your Linux skills? Let's get started.

Export Command in Linux Without Any Arguments

When you type export in your Linux terminal without adding anything else, it might seem like you're not doing much. But, actually, this simple action is quite powerful. It's like taking a peek into a secret list that your Linux system uses to work smoothly. This list contains environment variables, special settings that tell your programs how to behave.

Imagine you have a favorite game that needs to know where to save your progress. An environment variable can tell your game, "Hey, save my progress in this folder." When you use export without any arguments, Linux shows you all these special settings currently active in your shell, which is like your command control room.

Let's try this out. Open your terminal and simply type:

export

You'll see a bunch of text that might look confusing at first, but it's just Linux telling you about all the environment variables it's currently using. Each line you see is a rule that helps your programs know what to do.

Viewing All Exported Variables on Current Shell

Curious about what secrets your Linux shell is keeping? By using a special command, you can see all the 'exported' variables. These are like invisible helpers that guide your programs on how to act. For example, they can tell your computer where to look for certain files or how to display information.

To uncover these helpers, you open your terminal - it's like the command center for Linux - and type:

env


or

printenv


These commands are like magic spells that reveal all the exported variables in your shell. It's a bit like checking all the settings on your phone to see how everything is set up. You'll see a list with names like PATH, HOME, or USER, followed by some details. Each one of these is an instruction that helps your programs know where to find what they need or how to behave.

Don't worry if the list looks long or complex. It's just your Linux system making sure everything runs smoothly by keeping track of these settings.

Using Export with Functions

In Linux, functions are like little helpers that do specific tasks for you. You can create a function to do almost anything, from the simplest task like saying "Hello" to more complex ones like updating your system. But what if you want these helpers to be available every time you use the terminal, not just in one session? That's where export comes into play.

Let's say you have a function called greet. It's job is just to say "Hello, World!" like this:

greet() {
    echo "Hello, World!"
}


Now, you want greet to be known & usable in all parts of your terminal, even in new ones you open later. You can make this happen by using export with the -f option, which stands for 'function'. Like this:

export -f greet

After you do this, no matter where you are in your terminal or if you open a new one, typing greet will always give you a cheerful "Hello, World!" This makes your function a super-helper, always ready at your command.

To make our greet function even more useful, let's add a twist. Instead of always saying "Hello, World!" we'll make it greet anyone we tell it to. First, we'll change our function a bit:

greet() {
    echo "Hello, $1!"
}


In this new version, when you type greet followed by a name, it uses that name in the greeting. $1 is like a placeholder that takes the first word you type after greet and puts it into your greeting.

Now, to make sure this updated greet function is available everywhere in your terminal, you'll export it again like we did before:

export -f greet

Try it out! Type greet John, and it should say "Hello, John!" You can replace "John" with any name you like, and your function will greet that person.

By exporting the function like this, you make sure that this handy greeting tool is always at your fingertips, no matter where you are in the terminal or even if you open a new session.

Frequently Asked Questions

What does the export command do in Linux?

The export command in Linux makes variables and functions available to other programs and processes that start from your shell.

How can I see all the variables that are exported?

You can see all exported variables by typing export or env in the terminal. This shows you a list of all the settings your system is using.

Can I unexport a variable or function?

Yes, you can use export -n followed by the name of the variable or function. This will stop it from being exported to new processes.

Conclusion

The export command in Linux is a simple yet powerful tool that helps manage how your programs run and interact with each other. Whether you're setting up environment variables, making functions universally accessible, or tweaking settings with options like -p and -n, understanding export can significantly enhance your command line efficiency. By mastering these concepts, you're one step closer to becoming proficient in navigating the Linux environment.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass