-2

I want to delete an envrionment variable from background process which is sleep a little. I setted the "asd" variable with value "foo"

export asd=foo

after that I want to delete from background process. I try this, but doesn't work:

(sleep 3;unset asd;) &

When 3 seconds elapsed, the "export" command still show the previous setting. What do I wrong?

My goal is the "asd" variable removed after 3 seconds.

sarkiroka
  • 1,485
  • 20
  • 28
  • 3
    Subshell can't change its parent's environment. – oguz ismail Jul 19 '20 at 17:52
  • Okay, but how to do this? Remove the variable after a few seconds? – sarkiroka Jul 19 '20 at 18:00
  • 2
    You can't do it in a separate process; you have to do it in the shell process where you want the variable unset. Or you have to write your own shell that handles unsetting an environment variable from a child process somehow — maybe via multiple threads in the main shell with one thread that deals with `unset` requests from the child. Non-trivial. Your requirement isn't particularly reasonable; it is not supported by any shell that I know of (but I know I don't know about every possible shell). – Jonathan Leffler Jul 19 '20 at 18:11
  • 1
    Wait in your parent process with `wait` to finish its background process and then unset your variable asd. – Cyrus Jul 19 '20 at 18:13

1 Answers1

2

How to unset envrionment variable from background in linux?

You can set a trap in parent process and unset it inside the trap while setting the background process to deliver a signal after specified time.

asd=foo
trap 'unset asd' SIGUSR1
p=$BASHPID
( sleep 1; kill -SIGUSR1 $p ) &
echo $asd  # will print foo
sleep 2
echo $asd  # will print empty line

Note that it will not unset a variable "exactly" after the specified time, but when the handler for the signal gets executed.

I guess alternatively I could imagine patchin bash and writing a bash builtin command that would create a thread that after specified time would unset the variable. Note that setenv is not thread safe, so such setup would have to synchronized with other bash code.

What do I wrong?

You did unset the variable in a subshell. Subshell environment doesn't affect parent shell.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    What's the point of doing this anyway? Can you imagine an actual, practical use case for this? I can't. – oguz ismail Jul 19 '20 at 18:47
  • 1
    :) `What's the point of doing this anyway?` No idea, ask OP. `Can you imagine an actual, practical use case for this?` No. (I can imagine such code could exists as an extra spicy sauce in spaghetti code. Imagine environment variables magically get unset in the middle of the script...) – KamilCuk Jul 19 '20 at 18:52
  • I know right, it doesn't make any sense. Alas, *poor practice/bad idea* is not a valid close reason. – oguz ismail Jul 19 '20 at 18:59
  • 2
    @oguzismail I use a program which need a sensitive data from env variable. before I start, the variable is setted. after that I want to remove this sensitive data from env too. – sarkiroka Jul 19 '20 at 19:04
  • @sarkiroka Even if you remove it from the shell it will continue existing in the program's environment. If that's not a problem, instead of exporting the variable, set it only for the program, like `asd=fgh program`. – oguz ismail Jul 19 '20 at 19:08
  • It's okay in normal shell, but I used it from docker-compose env_file. how can I do to pass the sensitive data from host os env file to container, and after the program started remove from container? unset is just remove from current shell, not from the container, and every time when i login with "docker exec -it" the sensitive data is present – sarkiroka Jul 19 '20 at 19:14
  • 1
    In dockerfile use `ENV asd=`. It will set the variable to an empty string, which is enough to mask the variable value for later execution. Still it makes literally no sense to unset a variable after some time in docker, as the environment variable is set for all docker `RUN` and will persist and will be reset on each execution and is still readable from user by inspecting docker container. Aaand note that you asked XY question. ie. https://stackoverflow.com/questions/55789409/how-to-unset-env-in-dockerfile – KamilCuk Jul 19 '20 at 19:22
  • I not using own dockerfile, I just use a standard docker image from dockerhub with volume mapping and env setting. Is it wrong? Must I have use an own dockerfile? – sarkiroka Jul 19 '20 at 19:27