BASH

Define Global Environment Variables and Aliases

Create a Bash script to set up common environment variables and useful command aliases for all new shell sessions, enhancing developer productivity and consistency.

#!/bin/bash
PROFILE_FILE="$HOME/.bashrc" # Or ~/.zshrc for Zsh users

echo "Setting up environment variables and aliases in $PROFILE_FILE..."

# Check if a variable is already set before adding to avoid duplicates
if ! grep -q "export MY_APP_ENV=" "$PROFILE_FILE"; then
    echo 'export MY_APP_ENV="production"' >> "$PROFILE_FILE"
    echo "Added MY_APP_ENV."
fi

if ! grep -q "export PATH=\"$PATH:\\\$HOME/.bin\"" "$PROFILE_FILE"; then
    echo 'export PATH="$PATH:$HOME/.bin"' >> "$PROFILE_FILE"
    echo "Added ~/.bin to PATH."
fi

# Define common aliases
if ! grep -q "alias ll=" "$PROFILE_FILE"; then
    echo 'alias ll="ls -alF"' >> "$PROFILE_FILE"
    echo "Added 'll' alias."
fi

if ! grep -q "alias dev_start=" "$PROFILE_FILE"; then
    echo 'alias dev_start="cd /path/to/my/project && npm start"' >> "$PROFILE_FILE"
    echo "Added 'dev_start' alias."
fi

echo "Please run 'source $PROFILE_FILE' or restart your terminal to apply changes."
How it works: This script helps developers set up their shell environment by adding global environment variables and custom command aliases to their `.bashrc` (or `.zshrc`) file. It includes checks to prevent duplicate entries and demonstrates how to define common variables like `MY_APP_ENV` and useful aliases such as `ll` for listing files or `dev_start` for quickly launching a project. This enhances productivity and ensures consistent development setups.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs