EXPORT(1POSIX)

๐Ÿ’– EXPORT โ€” set the export attribute for variables ๐Ÿ’–

okay bestie, welcome to EXPORT(1POSIX) from the POSIX Programmer's Manual โœจ this is the page for the export special built-in, and it's lowkey one of the most important shell things you'll ever touch, no cap ๐Ÿซถ


๐Ÿšจ PROLOG ๐Ÿšจ

Real talk first, because this part matters: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the details of Linux behavior), or honestly the interface may not be implemented on Linux at all. It's giving "results may vary" ๐Ÿ’€


๐ŸŒธ NAME ๐ŸŒธ

export โ€” set the export attribute for variables. that's the whole vibe. one job. iconic.


โœจ SYNOPSIS โœจ

export name[=word]...

export -p

two moods, one built-in ๐Ÿ’…


๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

So here's the tea, bestie ๐Ÿต the shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. Basically you're telling the shell "hey, pass this along to everything I run next" and it's like "say less" ๐Ÿ”ฅ

If the name of a variable is followed by =word, then the value of that variable shall be set to word. so you can do both at once, queen behavior ๐Ÿ‘‘

The export special built-in shall support the Base Definitions volume of POSIX.1โ€2017, Section 12.2, Utility Syntax Guidelines. yes, that's a real citation, we're being thorough, we contain multitudes ๐ŸŽ€

When -p is specified, export shall write to the standard output the names and values of all exported variables, in the following format:

"export %s=%s\n", <name>, <value>

if name is set, and:

"export %s\n", <name>

if name is unset. so basically it dumps everything back out in a form you could feed right back in, which is honestly so convenient, main character energy โญ

The shell shall format the output, including the proper use of quoting, so that it is suitable for reinput to the shell as commands that achieve the same exporting results, except โ€” and listen, there are two exceptions, don't skip them:

  1. Read-only variables with values cannot be reset. nope. not happening. read-only means read-only ๐Ÿ™ˆ
  2. Variables that were unset at the time they were output need not be reset to the unset state if a value is assigned to the variable between the time the state was saved and the time at which the saved output is reinput to the shell. (i.e. if you snoop around and set it in the middle, the restore isn't obligated to un-set it. it's giving "you did this to yourself" ๐Ÿ’€)

When no arguments are given, the results are unspecified. unspecified!! which is a fancy way of saying "we're not telling you what happens, don't rely on it" ๐Ÿ˜ญ


๐ŸŽ€ OPTIONS ๐ŸŽ€

See the DESCRIPTION. yeah, the -p is up there, we covered it, go look ๐Ÿ‘€


๐Ÿฆ‹ OPERANDS ๐Ÿฆ‹

See the DESCRIPTION. also up there. the name[=word]... situation. you got this ๐Ÿ’–


๐ŸŒˆ STDIN ๐ŸŒˆ

Not used. it's not even invited to the party.


๐Ÿ’ซ INPUT FILES ๐Ÿ’ซ

None. nothing to see here.


๐ŸŒธ ENVIRONMENT VARIABLES ๐ŸŒธ

None. the irony of the environment-variables page having no environment variables is not lost on us, bestie ๐Ÿ˜ญ


โšก ASYNCHRONOUS EVENTS โšก

Default. standard behavior, no drama.


๐Ÿซถ STDOUT ๐Ÿซถ

See the DESCRIPTION. (it's the -p output, the "export %s=%s\n" format, that whole gorgeous thing โœจ)


๐Ÿ’€ STDERR S ๐Ÿ’€

The standard error shall be used only for diagnostic messages. so if it's yelling at you, that's a real message, pay attention ๐Ÿšจ


๐ŸŽ€ OUTPUT FILES ๐ŸŽ€

None.


๐Ÿงš EXTENDED DESCRIPTION ๐Ÿงš

None. there is no extended description. we said what we said ๐Ÿ’…


๐Ÿ‘‘ EXIT STATUS ๐Ÿ‘‘

  • 0 โ€” All name operands were successfully exported. slay ๐Ÿ’–
  • >0 โ€” At least one name could not be exported, or the -p option was specified and an error occurred. it's giving "we have a problem" ๐Ÿ˜ญ

๐Ÿ’œ CONSEQUENCES OF ERRORS ๐Ÿ’œ

Default.

The following sections are informative. (so like, vibe-wise they're context, not law, iykyk)


๐Ÿ”ฅ APPLICATION USAGE ๐Ÿ”ฅ

okay this one is genuinely important, listen up bestie ๐Ÿ“ข

Note that, unless X was previously marked readonly, the value of "$?" after:

export X=$(false)

will be 0 (because export successfully set X to the empty string) and that execution continues, even if set -e is in effect. so like โ€” the command substitution failed, but export itself succeeded, so $? is happy and set -e doesn't trip. sneaky, right?? ๐Ÿ’€

In order to detect command substitution failures, a user must separate the assignment from the export, as in:

X=$(false)
export X

periodt. do it in two steps if you actually want to catch the failure ๐Ÿ™Œ


๐ŸŒŸ EXAMPLES ๐ŸŒŸ

Export PWD and HOME variables:

export PWD HOME

Set and export the PATH variable:

export PATH=/local/bin:$PATH

Save and restore all exported variables:

export -p > temp-file
unset a lot of variables
... processing
. temp-file

the save-and-restore one is chef's kiss, it's literally a little snapshot of your shell environment ๐ŸŽ€โœจ


๐Ÿง  RATIONALE ๐Ÿง 

Some historical shells use the no-argument case as the functional equivalent of what is required here with -p. This feature was left unspecified because it is not historical practice in all shells, and some scripts may rely on the now-unspecified results on their implementations. Attempts to specify the -p output as the default case were unsuccessful in achieving consensus. The -p option was added to allow portable access to the values that can be saved and then later restored using; for example, a dot script.

so yeah, the reason no-args is unspecified is literally "we could not all agree" ๐Ÿ’€ committee vibes, but make it POSIX.


๐Ÿ”ฎ FUTURE DIRECTIONS ๐Ÿ”ฎ

None. the future is now, and it looks exactly like the present ๐ŸŒˆ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

  • Section 2.14, Special Built-In Utilities
  • The Base Definitions volume of POSIX.1โ€2017, Section 12.2, Utility Syntax Guidelines

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page bestie ๐Ÿซถ brought to you by IEEE/The Open Group, 2017 โ€” thanks for reading, now go export something and make it everyone's problem ๐Ÿ’…โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from