How to run a script in the existing shell

In this post, we will answer following questions.

How to run a script in existing shell? or
How to load variables in a script to current(user) shell? or
How to easily load variables into start python virtualenv?

 

Why should you learn this?

It’s not always necessary but, sometimes when we are working on multiple projects, it would be wise to define all your environmental variables in once place and use them when required. Of course, you can define them in you .bash profile or anything other profile you are using but think again how good it is to define in your .bash shell? You will overload your shell as well as other people who want use that code later, will also the do same.

Loading variables all with in your shell profiles will again effect your other projects. For my case, I have Python and Anaconda. Anaconda, comes with its own python setup and packages set. Now when I load python, anaconda python loads slowly.

So I follow, following the tradition of setting up an env_settings.sh files to help me. Just because I do, I can’t & I won’t ask you follow it. This is post aim to let you know there is an option.

 

In a Unix terminal, when we try to run a script they run in a different shell and finished the job. For example

#!/bin/bash
# author = msampathkumar
# purpose = to import settings for local python, pip & virtualenv
export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
source env/bin/activate
clear

This script is to load my local variables for the application and activate its respective virtual environment. Unfortunately, when I run it with below command it does not give the desired output.

SDS-bash3.2$ sh settings.sh
SDS-bash3.2$

In such cases, we can use the following method.

SDS-bash3.2$ . ./settings.sh
(env)SDS-bash3.2$

Source:http://unix.stackexchange.com/questions/102838/run-script-in-current-shell-without-before-command

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s