PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 21, 2022

[FIXED] How to source stdout in bash?

 August 21, 2022     bash, environment-variables, macos, python     No comments   

Issue

I am having trouble exporting environmental variables from stdout.

What do I do wrong and how to fix it?

$ cat env.py 
print("export TEST_ME=123")

$ python env.py
export TEST_ME=123

$ echo $(python env.py)
export TEST_ME=123

$ echo $(python env.py) | . /dev/fd/0
bash: echo: write error: Broken pipe

$ echo "export TEST_ME=123" | . /dev/fd/0 && echo $TEST_ME

$

Same command works in zsh

$ zsh
% echo "export TEST_ME=123" | . /dev/fd/0 && echo $TEST_ME
123
%

Update:

Python3.9 on macOS gets that broken pipe, so I am trying with just echo

$ bash --noprofile --norc
bash-3.2$ . <(echo "export TEST_ME=123") && echo $TEST_ME

bash-3.2$ . <(python3.9 env.py)
bash-3.2$ Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

bash-3.2$ . <(python2 env.py)
bash-3.2$ close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

bash-3.2$

Solution

Use process substitution:

$ . <(python env.py)
$ echo $TEST_ME
123

The way this works is: it creates a temporary pipe in the filesystem, and connects stdout of python env.py to the pipe. The <(...) construct itself is then replaced by the file name of that pipe.



Answered By - Thomas
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing