Skip to main content

python-shell-source

A python module for sourcing variables from shell scripts.

Installation

You can install this package with pip or conda.

$ pip install shell-source
$ conda install -c abrahammurciano shell-source

Documentation

The full documentation is available here

Usage

This module provides a function source which attempts to mimic the shell's source command.

The purpose of this function is to allow you to run a shell script which sets either environment variables or local variables, and then give you access to those variables. Normally this is not a straght-forward task, but this function achieves it by running the script in its intended shell then injecting commands to the shell to write its local variables and its environment variables to a temporary file. Finally it reads the temporary file and parses it to return to you with exactly the data you asked for.

Basic Usage

If you just pass a script and an interpreter you'll get back all the environment variables and local variables visible to and set by the script.

>>> from shell_source import source
>>> variables = source("path/to/script.sh", "bash")
>>> # It returns a dictionary of local and environment variables known by the script.
>>> variables
{"USER": "abraham", "PATH": "/bin:/usr/bin", ..., "foo": "bar"}

Requesting Specific Variables

If you specify the argument variables, then only those variables you passed will be present as keys in the returned dictionary.

>>> source("path/to/script.sh", "zsh", variables=("foo", "bar", "biz"))
{"foo": ..., "bar": ..., "biz", ...}

Ignoring Local Variables

If you don't want to obtain any local variables set by the script, but only want the environment variables, you can pass ignore_locals=True.

Passing Arguments to the shell

If you want to pass arguments to the shell, for example -x or -e, you can pass it directly in the shell argument.

>>> source("path/to/script.sh", "bash -x")

Passing Arguments to the script

If you want to pass arguments to the script being sourced, you can pass them in the args argument.

>>> source("path/to/script.sh", "bash", args=("foo", "bar"))

Supporting Different Shells

This module has been tested to work with bash, zsh, and ksh out of the box. You can use any other shell that's somewhat posix compliant, but it it doesn't work, you may have to create a class derived from the Scripter class to indicate to source how to interact with the shell you want to use.

Some specialized implementations of Scripter are provided in shell_source.scripters for shells that are not posix compliant, such as csh, tcsh and fish.

For example, to use tcsh to source a script, you can use the CshScripter class like so:

>>> source("path/to/script.csh", "tcsh", scripter=CshScripter())

Metadata

Release files for shell-source 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shell-source 3.0.0
File Size Uploaded
shell_source-3.0.0.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shell-source 3.0.0
File Interpreter ABI Platform
shell_source-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / shell_source-3.0.0.tar.gz

Download URL shell_source-3.0.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
adbc3967c01aad4d8e94fa0c5a49dc6782689c003b639fc9af0f5ea822ab0543
BLAKE2b-256 checksum
How to use checksums
a80bc4ee6404c2039dad0afadcf2e54aa95d721b687c91c8ed73da59843b58b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.0 CPython/3.7.16 Linux/5.15.0-1037-azure

Release files / shell_source-3.0.0-py3-none-any.whl

Download URL shell_source-3.0.0-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
79e0bbe02e1de81b43336a739ec14033e85eb863b23d53139fb79eaffab28266
BLAKE2b-256 checksum
How to use checksums
b39e0042ea43a1e4092969f25f675aab7d26150fb32ed5ff49568d587f317a67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.0 CPython/3.7.16 Linux/5.15.0-1037-azure

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page