-2

How to export a forked variable in a script in block (bash)?

If you make a variable in block group forked, how to get the variable back?

unset VAR; { VAR=$( echo "AAAA" ) ;} & PID=$!; echo "[$PID - $VAR]";

gives back only the PID

[31270 - ]

How to do it without external files?

Toto
  • 89,455
  • 62
  • 89
  • 125
  • 2
    `(direct shell)` you had variable `VAR` already set before you run the command. `unset VAR` before running. Ie. - cannot reproduce, I see only VAR in both case. `How to do it?` Write the value to a file. Read the file from parent shell. – KamilCuk Jun 05 '21 at 20:29
  • @KamilCuk the same also if i do ```unset VAR```. Edited also the question & added to command to complete it. I don't want to use external file. A solution with only variable plz – Santoo Death XXXXXXXXXXXXXX Jun 05 '21 at 21:58
  • I have also tried pasting in your exact code, in the "direct shell" example, and it doesn't work. Please try it again. – psmears Jun 05 '21 at 22:03
  • @psmears "I have also tried pasting in your exact code, in the "direct shell" example, and it doesn't work. Please try it again." I edited the question time ago – Santoo Death XXXXXXXXXXXXXX Jun 05 '21 at 23:02
  • @KamilCuk "How to do it without external files? What have you tried and what research did you do?" I think u are talking me superficially. Thank tha same. – Santoo Death XXXXXXXXXXXXXX Jun 05 '21 at 23:03
  • You could use named pipes https://www.linuxjournal.com/content/using-named-pipes-fifos-bash. – Nic3500 Jun 06 '21 at 02:05
  • @Nic3500 "Does this answer your question? Inter-process communication without FIFOs" no. the question it's closed. "You could use named pipes linuxjournal.com/content/using-named-pipes-fifos-bash" don't want pipes. – Santoo Death XXXXXXXXXXXXXX Jun 06 '21 at 05:05
  • @Santoo: When I wrote my comment you had still not made your edit. I have posted an answer; I hope it is useful. – psmears Jun 06 '21 at 13:12

1 Answers1

0

Short answer: you can't - it's impossible.

Once the subshell { VAR=$( echo "AAAA" ) ;} is forked off with &, it is a separate process, the child process. It inherits a copy of all variables in the original shell, but any or changes to those variables, or new variables it creates, remain within the child, and cannot be seen by the original (parent) shell. What's more, since you're not waiting for the child to finish, there is no guarantee that the child will even have set the variable at the point the parent wants to use it.

The only way to get the data back from the child to the parent is for the processes to communicate - this is called inter-process communication (IPC). There are many ways of doing this, with different characteristics, and hence different suitability for different tasks (and for use within the shell).

You say you don't want to use files or a pipe. I'm not sure why that is (it would be helpful if you said in your question), but other methods of IPC include:

  • Sockets (unix domain, TCP etc)
  • FIFOs
  • Shared memory
  • Signals
  • Message queues
  • ptrace
  • etc...

Pick one :)

psmears
  • 26,070
  • 4
  • 40
  • 48
  • 1
    "Sockets FIFOs Shared memory Signals Message queues ptrace Pick one :)". Child processes and parents processes are making me crazy. Variables too. Think your answer it's correct. Tried so much but it's impossible. Shared memory dir changes from OS to OS. Signals and ptrace don't know what are (i've to study). With pipe (without external files) i don't know how to do it. I think the only one answer it's Sockets or FIFOs. Also external files are difficult to place. Working on that. (ACCEPTED). Thanks – Santoo Death XXXXXXXXXXXXXX Jun 06 '21 at 19:15