What are the differences between set, env, and export commands in Linux?

Understanding the Differences: set, env, and export Commands in Linux

In the Linux operating system, the set, env, and export commands are used to manage environment variables, which are essential for configuring and running applications. These commands have distinct purposes and functionalities, and understanding the differences between them is crucial for effectively managing your Linux environment.

set Command

The set command is used to display and manipulate shell variables. It can be used to set, unset, or display the values of shell variables. When executed without any arguments, the set command will display all the shell variables and their corresponding values.

Example:

$ set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="5" [1]="1" [2]="4" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
BASH_VERSION='5.1.4(1)-release'
COLORTERM=truecolor
COLUMNS=80
COMP_WORDBREAKS=$' \t\n"\'><=;|&(:'
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DESKTOP_SESSION=gnome
DISPLAY=:0
EUID=1000
GDMSESSION=gnome
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_SHELL_SESSION_ID=c2a7d7d1a5c94d3b9d3f3a3b3c3d3e3
# ... (more output)

The set command can also be used to set shell variables. For example, to set a variable named MY_VAR with the value "Hello, World!", you can use the following command:

$ set MY_VAR="Hello, World!"

env Command

The env command is used to display the current environment variables or to run a command in a modified environment. When executed without any arguments, the env command will display all the environment variables and their corresponding values.

Example:

$ env
SHELL=/bin/bash
SESSION_MANAGER=local/myhost:@/tmp/.ICE-unix/1234,unix/myhost:/tmp/.ICE-unix/1234
QT_ACCESSIBILITY=1
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg
XDG_SESSION_DESKTOP=gnome
TERM=xterm-256color
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GTK_IM_MODULE=fcitx
LANGUAGE=en_US:en
DESKTOP_SESSION=gnome
LOGNAME=myuser
PWD=/home/myuser
XMODIFIERS=@im=fcitx
HOME=/home/myuser
LANG=en_US.UTF-8
IM_CONFIG_PHASE=1
GDMSESSION=gnome
MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path
# ... (more output)

The env command can also be used to run a command in a modified environment. For example, to run the ls command with a different PATH environment variable, you can use the following command:

$ env PATH="/usr/local/bin:/usr/bin:/bin" ls

This will run the ls command with the specified PATH environment variable, which may be different from the current shell's PATH.

export Command

The export command is used to make a shell variable available to child processes. When a variable is exported, it becomes an environment variable that can be accessed by any child processes spawned by the shell.

Example:

$ MY_VAR="Hello, World!"
$ export MY_VAR
$ echo $MY_VAR
Hello, World!

In the above example, we first set a shell variable MY_VAR to the value "Hello, World!". Then, we use the export command to make this variable available to child processes. Finally, we can access the value of the MY_VAR environment variable using the echo command.

The export command can also be used to set and export a variable in a single step:

$ export MY_VAR="Hello, World!"
$ echo $MY_VAR
Hello, World!

In summary, the set command is used to manage shell variables, the env command is used to manage environment variables and run commands in a modified environment, and the export command is used to make shell variables available to child processes as environment variables.

graph TD A[Shell] B[Environment] C[Child Processes] A -- "set" --> A A -- "export" --> B B -- "env" --> C B -- "env" --> A

By understanding the differences between these commands, you can effectively manage your Linux environment and ensure that your applications and scripts have access to the necessary variables and configurations.

0 Comments

no data
Be the first to share your comment!