setenv - Man Page

change or add an environment variable

Library

Standard C library (libc, -lc)

Synopsis

#include <stdlib.h>

int setenv(const char *name, const char *value, int overwrite);
int unsetenv(const char *name);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

setenv(), unsetenv():

    _POSIX_C_SOURCE >= 200112L
        || /* glibc <= 2.19: */ _BSD_SOURCE

Description

The setenv() function adds the variable name to the environment with the value value, if name does not already exist. If name does exist in the environment, then its value is changed to value if overwrite is nonzero; if overwrite is zero, then the value of name is not changed (and setenv() returns a success status). This function makes copies of the strings pointed to by name and value (by contrast with putenv(3)).

The unsetenv() function deletes the variable name from the environment. If name does not exist in the environment, then the function succeeds, and the environment is unchanged.

Return Value

setenv() and unsetenv() functions return zero on success, or -1 on error, with errno set to indicate the error.

Errors

EINVAL

name is NULL, points to a string of length 0, or contains an '=' character.

ENOMEM

Insufficient memory to add a new variable to the environment.

Attributes

For an explanation of the terms used in this section, see attributes(7).

InterfaceAttributeValue
setenv(), unsetenv()Thread safetyMT-Unsafe const:env

Standards

POSIX.1-2008.

History

POSIX.1-2001, 4.3BSD.

Prior to glibc 2.2.2, unsetenv() was prototyped as returning void; more recent glibc versions follow the POSIX.1-compliant prototype shown in the Synopsis.

Caveats

POSIX.1 does not require setenv() or unsetenv() to be reentrant.

Bugs

POSIX.1 specifies that if name contains an '=' character, then setenv() should fail with the error EINVAL; however, versions of glibc before glibc 2.3.4 allowed an '=' sign in name.

See Also

clearenv(3), getenv(3), putenv(3), environ(7)

Referenced By

clearenv(3), ecalloc(3), environ(7), extraclangtools(1), getenv(3), guestfs-release-notes-1.50(1), libsystemd(3), libudev(3), perlapi(1), pmGetConfig(3), putenv(3), sd-bus(3), sd_bus_add_match(3), sd_bus_add_node_enumerator(3), sd_bus_add_object(3), sd_bus_add_object_manager(3), sd_bus_attach_event(3), sd_bus_call(3), sd_bus_call_method(3), sd_bus_can_send(3), sd_bus_close(3), sd_bus_creds_get_pid(3), sd_bus_creds_new_from_pid(3), sd_bus_default(3), sd_bus_emit_signal(3), sd_bus_enqueue_for_read(3), sd_bus_error(3), sd_bus_error_add_map(3), sd-bus-errors(3), sd_bus_get_current_handler(3), sd_bus_get_fd(3), sd_bus_get_name_creds(3), sd_bus_get_name_machine_id(3), sd_bus_interface_name_is_valid(3), sd_bus_is_open(3), sd_bus_list_names(3), sd_bus_message_append(3), sd_bus_message_append_array(3), sd_bus_message_append_basic(3), sd_bus_message_append_string_memfd(3), sd_bus_message_append_strv(3), sd_bus_message_at_end(3), sd_bus_message_copy(3), sd_bus_message_dump(3), sd_bus_message_get_cookie(3), sd_bus_message_get_monotonic_usec(3), sd_bus_message_get_signature(3), sd_bus_message_get_type(3), sd_bus_message_new(3), sd_bus_message_new_method_call(3), sd_bus_message_new_method_error(3), sd_bus_message_new_signal(3), sd_bus_message_open_container(3), sd_bus_message_read(3), sd_bus_message_rewind(3), sd_bus_message_seal(3), sd_bus_message_sensitive(3), sd_bus_message_set_destination(3), sd_bus_message_set_expect_reply(3), sd_bus_message_skip(3), sd_bus_message_verify_type(3), sd_bus_negotiate_fds(3), sd_bus_new(3), sd_bus_path_encode(3), sd_bus_process(3), sd_bus_query_sender_creds(3), sd_bus_reply_method_error(3), sd_bus_reply_method_return(3), sd_bus_request_name(3), sd_bus_send(3), sd_bus_set_address(3), sd_bus_set_close_on_exit(3), sd_bus_set_connected_signal(3), sd_bus_set_description(3), sd_bus_set_exit_on_disconnect(3), sd_bus_set_fd(3), sd_bus_set_method_call_timeout(3), sd_bus_set_property(3), sd_bus_set_sender(3), sd_bus_set_server(3), sd_bus_set_watch_bind(3), sd_bus_slot_get_bus(3), sd_bus_slot_ref(3), sd_bus_slot_set_description(3), sd_bus_slot_set_destroy_callback(3), sd_bus_slot_set_floating(3), sd_bus_slot_set_userdata(3), sd_bus_start(3), sd_bus_track_add_name(3), sd_bus_track_new(3), sd_bus_wait(3), sd-daemon(3), sd-device(3), sd_device_get_syspath(3), sd-event(3), sd_event_add_child(3), sd_event_add_defer(3), sd_event_add_inotify(3), sd_event_add_io(3), sd_event_add_memory_pressure(3), sd_event_add_signal(3), sd_event_add_time(3), sd_event_exit(3), sd_event_get_fd(3), sd_event_new(3), sd_event_now(3), sd_event_run(3), sd_event_set_signal_exit(3), sd_event_set_watchdog(3), sd_event_source_get_event(3), sd_event_source_get_pending(3), sd_event_source_set_description(3), sd_event_source_set_destroy_callback(3), sd_event_source_set_enabled(3), sd_event_source_set_exit_on_failure(3), sd_event_source_set_floating(3), sd_event_source_set_prepare(3), sd_event_source_set_priority(3), sd_event_source_set_ratelimit(3), sd_event_source_set_userdata(3), sd_event_source_unref(3), sd_event_wait(3), sd_get_seats(3), sd-hwdb(3), sd_hwdb_get(3), sd_hwdb_new(3), sd-id128(3), sd_id128_get_machine(3), sd_id128_randomize(3), sd_id128_to_string(3), sd-journal(3), sd_journal_add_match(3), sd_journal_print(3), sd_listen_fds(3), sd-login(3), sd_login_monitor_new(3), sd_machine_get_class(3), sd_notify(3), sd_path_lookup(3), sd_seat_get_active(3), sd_session_is_active(3), sd_uid_get_state(3), sd_watchdog_enabled(3), stress-ng(1), sudo_plugin(5), ul(1).

The man page unsetenv(3) is an alias of setenv(3).

2024-05-02 Linux man-pages 6.9.1