Bash scripts are a Linux developer’s secret weapon — they help automate repetitive tasks, simplify complex workflows, and keep environments consistent. Whether you're managing development environments, deploying to servers, or working with containers and cloud services, bash scripting makes life easier.
Linux users — especially developers, DevOps engineers, and sysadmins — often find themselves writing small, handy scripts to boost their productivity. So from today, I’m kicking off a series of such small but useful Bash scripts that can save time and reduce effort in everyday development.
🚀 Version v.1.0: Quick-Select a Python Virtual Environment
In this first post of the series, let’s create a simple script that helps you easily choose and activate a Python virtual environment — without navigating into folders every time.
Older way
Every time we work with Python, a virtual environment is usually essential. Like many of you, I have multiple virtual environments — each tailored for a different project.
To activate one, we typically do:
source ENVIRONMENT_NAME/bin/activate
But this can be improved — especially if you want a cleaner, more organized, and faster workflow.
📁 Step 1: Organize Your Environments
First, keep all your virtual environments in a single directory — say, venv. This way:
- Your workspace stays clean and manageable.
- You always know where to look for your environments.
🛠️ Step 2: A Bash Script to Help
Now, here’s a basic script to list and activate a virtual environment from your organized folder:
#!/bin/bash
list="$(ls venv)"
echo $list
echo Which environment do you want?
read env
response="$(source venv/$env/bin/activate 2>&1)"
if [[ $? -eq 0 ]] ; then
echo "Activating Virtual Environment..."
source venv/$env/bin/activate
else
echo "Error: $response"
fi
This is actually just a basic script. What it is doing is that:
list="$(ls venv)"
echo $list
- This line just lists out all the virtual envs in the folder and prints it to the console.
echo Which environment do you want?
read env
response="$(source venv/$env/bin/activate 2>&1)"
This will ask "which environment we want to activate".
The lineread env
will read the output from the console, it actually holds the value of the environment we have selected and is used in the lineresponse="$(source venv/$env/bin/activate 2>&1)
as a variable to be used to activate the environment and store that value in another variable known asresponse
.response="$(source venv/$env/bin/activate 2>&1)
This line is actually used for error handling. What happens is that when we dosource venv/$env/bin/activate
, if the selected env is valid that is with no spelling mistakes or does actually exits, it will result as an exit code of 0 with no errors and will continue further.
But if the value is wrong then it will result in an error causing the script to exit abruptly.
Hence to prevent this Redirecting of Output is used. 2 stands forstderr
and 1 forstdout
.
The statement '2>&1, says that if error occurs redirect that to the
stdout` only. And that adds the the result to the response variable.
if [[ $? -eq 0 ]] ; then
echo "Activating Virtual Environment..."
source venv/$env/bin/activate
else
echo "Error: $response"
fi
(Don't know why this couldn't get into a code block)
This is an if statement, if the exit code(or the $?) is equal to (-eq) 0, then activate the virtual env.
Or if the exit code is not equal to 0 then give the error message in the output.
Running the script
This is an interesting part. As some of you might think that this can run easily as a executable script. But it will not.
Using ./pyenv.sh
will run the script in a new shell- a subshell is created. This will result into activation of no venv as we want it to be in the current shell where we are working.
So, to remove this we use either source pyenv.sh
or . pyenv.sh1
to tell the os that run the script in the current shell.