Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:17:40+00:00 2026-05-30T04:17:40+00:00

I am trying to source a bash script containing some environment variables in python.

  • 0

I am trying to source a bash script containing some environment variables in python. I followed one other thread to do it. But, there seems that one of the variable is malformed, as can be seen in the given snippet.

COLORTERM=gnome-terminal
mc=() {  . /usr/share/mc/mc-wrapper.sh
}
_=/usr/bin/env

I am using the following code to set up the current environment.

import os
import pprint
import subprocess

command = ['bash', '-c', 'source init_env && env']

proc = subprocess.Popen(command, stdout = subprocess.PIPE)

for line in proc.stdout:
    (key, _, value) = line.partition("=")
    os.environ[key] = value

proc.communicate()

‘

If I change the above code a little like putting a condition:

for line in proc.stdout:
    (key, _, value) = line.partition("=")
    if not value:
        continue
    os.environ[key] = value

then things are working but the environment is corrupted because of one missing bracket as can be seen from the snippet of environment variable that the bracket is appearing on new line. Because of this corruption, If I run some other command like

os.system("ls -l")

it gives me the following error

sh: mc: line 1: syntax error: unexpected end of file
sh: error importing function definition for `mc'

What could be the possible solutions for this problem?

Thanks alot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:17:41+00:00Added an answer on May 30, 2026 at 4:17 am

    Probably the best way to do this is to create a separate program that writes out the environment variables in a way that is easily and unambiguously processed by your own program; then call that program instead of env. Using the standard pickle module, that separate program can be as simple as this:

    import os
    import sys
    import pickle
    
    pickle.dump(os.environ, sys.stdout)
    

    which you can either save into its own .py file, or else put directly in a Bash command:

    python -c 'import os, sys, pickle; pickle.dump(os.environ, sys.stdout)'
    

    In either case, you can process its output like this:

    import os
    import pprint
    import subprocess
    import pickle
    
    command = [
          'bash',
          '-c',
          'source init_env && ' +
             'python -c "import os, sys, pickle; ' +
                        'pickle.dump(os.environ, sys.stdout)"'
    ]
    
    proc = subprocess.Popen(command, stdout = subprocess.PIPE)
    
    for k, v in pickle.load(proc.stdout).iteritems():
        os.environ[k] = v
    
    proc.communicate()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a shell script that, when run, will set some environment
I am trying to activate my Virtual Python Environment to use with Pylons but
Not that I'm trying to prevent 'View Source' or anything silly like that, but
I'm trying to find open source profilers rather than using one of the commercial
I'm trying to write (what I thought would be) a simple bash script that
I'm trying to copy files to the current directory using a bash script. In
A third-party Python 2.5 script I'm trying to debug has got me stymied. The
I'm trying to rsync files between two servers with rsync -avlzp /source user@server:/destination but
really struggling with this one. I've recently setup a bash shell script to extract,
In the process of trying to write a Python script that uses PIL today,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.