0

I'm relatively new to Bash and unable to explain my problem in the title. I've done a lot of googling and I guess that's the title I came up with.

I want to be able to use Python EOF and define a Bash variable in the EOF (if possible) and call it after.

File test.txt

everything - literally the string everything

And I'm opening this file and getting contents with

File test.sh

#!/bin/bash
CMD=$(cat <<EOF

with open('text.txt', 'r') as f:
    for line in f.readlines():
          pass

print(f"WOW text has {line}")

EOF
)

python3 -c "$CMD"

Output:

WOW text has everything

I want to be able to share a variable by defining it in my CMD (I don't know what it's called) and echo it in Bash after it's done;

#!/bin/bash
CMD=$(cat <<EOF

with open('text.txt', 'r') as f:
    for line in f.readlines():
          pass

print(f"WOW text has {line}")
$var = line - somehow define a Bash variable in Python EOF
EOF
)

python3 -c "$CMD"
echo $var - output this

So then the new output (of what I want) is:

WOW text has everything
everything
7
  • You are assigning a python program to CMD, but attempting to invoke python with $PYCMD. Commented Jan 31, 2022 at 20:39
  • opps lol, but that's not the problem tho Commented Jan 31, 2022 at 20:57
  • The $CMD data is invoked inside python3, you can't set a variable in python and expect to see the value in the shell. You can capture the output from the python command into a variable e.g. var=$(python3 -c "$CMD"). Commented Jan 31, 2022 at 21:01
  • 3
    I think what you're calling "EOF" is a here-document -- "EOF" is just the delimiter you happen to be using to indicate the end of the here-document. As for defining a shell variable from a python program (wherever it's stored), you can't -- at least, not directly. Python runs as a subprocess of the shell, and has no ability to modify the shell's environment or internal state. Commented Jan 31, 2022 at 21:07
  • 1
    You capture the printed output from the python script. Commented Jan 31, 2022 at 21:51

2 Answers 2

2

I want to be able to share a variable by defining it in my CMD

That is not possible.

I want to

Write a Bash builtin or path Bash with a modification that creates a shared memory region and introduces a new special variable that uses that shared memory region to store and fetch the value of the variable.

Use the shared memory region from python to store the value of the variable.

You could also go three steps further, and just straight introduce a Bash loadable module with interface to Python with communication via a local socket or similar, similar to vim Python library.


You can:

  • Output the value and use a command substitution to capture the output of python process and assign that output to a variable.

    var=$(python -c 'print(line)')
    echo "$var"
    
  • Store the value to a file and then read the content of that file in Bash. Or similar.

    python -c 'open("/tmp/temporaryfile.txt").write(line)'
    var=$(cat /tmp/temporaryfile.txt)
    echo "$var"
    

You may want to research "processes" - what they are, what do they share and what they don't share, and what are the methods of communication between processes.

Sign up to request clarification or add additional context in comments.

Comments

0

This solution might be complex, but it is working in my Ubuntu instance.

One line of a Python script can

`

$
$ test_var=$(python3 -c "import yaml,sys; yaml_as_dict=(lambda :yaml.safe_load(open(f'{sys.argv[1]}','r').read()))()[sys.argv[2]][sys.argv[3]]; print(yaml_as_dict)" <argv1> <argv2> <argv3>)
$
$ echo $test_var
$ 

3 Comments

Why would you use $"" here? That's localization support -- looking up your string in a gettext translation table. Surely you don't have your Python code translated to different languages?
More to the point, what part of the OP's question requires yaml or any of the other complexity you've added? They're trying to have Python code set arbitrary shell variables, but here you're setting exactly one shell variable by reading the output, nothing nearly as fancy or integrated as what they're asking for. Moreover, they want to specify the variable name inside the heredoc, but here you're setting it in the code that invokes the Python interpreter.
Thank you, @Charles Duffy, for pointing out the unnecessary use of localization strings ($""). I shared the snippet from a larger, dynamic scripting setup where managing multiple scripts efficiently is crucial and execution speed is prioritized. This example demonstrates a specific function adapted from my broader work. My code might look complex, but it was handy in my case. You can use your version of Python code, but I provided a sample version that effectively answers the original query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.