How to run script in same session without exec?

0

I have below script to set me in my project:-

#! /bin/zsh

source ~/.zshrc
crld #alias for Project base
cd Kenneth_Love/survivalguide
workon kenneth_tutorial

This script works but doesn't drop me in virtualenv(alias is properly set)

Virtualenv as the name suggests create virtualenv which is used to keep environment separately manageable and so it doesn't interfere with each other for example somewhere you want x version of a package and for other project you want Y, so it makes doing that easy. Running above script commands is repetitive task, but this script run under different session and then exit. I want it to run on the same shell env where I am calling this script.

user1517650

Posted 2016-03-27T07:57:00.507

Reputation: 1

virtualenv appears not to be a frequently-discussed topic in this community.  Can you please explain briefly what it is, and describe more specifically what the script does and does not do (what do you want it to do?), and what alias you're talking about?  Please do not respond in comments; [edit] your question to make it clearer and more complete. – G-Man Says 'Reinstate Monica' – 2016-03-27T08:11:17.250

Answers

1

Some shell commands (such as cd) affect the current shell and its child/descendent processes, but cannot affect the parent shell.  When you run a script as an ordinary command (e.g., scriptname.sh or ./scriptname.sh), it runs in a separate shell process.  When that shell process terminates, the effects of those commands go away.  That's why scripts like yours often end with exec sh, exec bash, exec zsh, or whatever — it causes the process that's in the new environment to persist, as an interactive shell.

If you don't want to do it that way, the only other approach is to run the script in your main, interactive shell.  In bash, you can do this with

source scriptname.sh

or

. scriptname.sh

(You may need to use ./scriptname.sh if the script is in the current directory.)  I'm pretty sure zsh has the same functionality.  The syntax might be different; check the documentation for your shell.

If you have trouble remembering to do this (or if it gets cumbersome), considering defining an alias for it.

Scott

Posted 2016-03-27T07:57:00.507

Reputation: 17 653