Linux: Edit Environment Variables on the Fly with varedit

The varedit function is a versatile and user-friendly Bash function designed to simplify editing environment variables in memory using…

Linux: Edit Environment Variables on the Fly with varedit
Photo by Wesley Ford on Unsplash

The varedit function is a versatile and user-friendly Bash function designed to simplify editing environment variables in memory using vipe, a utility from the moreutils package. With varedit, you can create or modify environment variables dynamically during your terminal session.

Overview of the Function

The varedit function creates or modifies environment variables in memory. Changes made using this function are session-specific and will not persist after the terminal session ends.

The varedit function performs the following tasks:

  1. Check for vipe Installation: Ensures vipe is installed on the system before proceeding.
  2. Verify Input: Confirms the function is called with exactly one parameter, the name of the environment variable to edit.
  3. Handle Non-Existent Variables: If the specified variable does not exist, it creates it as an empty variable.
  4. Edit Using vipe: Opens the current value of the environment variable in vipe for editing.
  5. Handle Changes: Updates the variable with the new value if changes are made. If no changes are made and the variable was created as empty, it unsets the variable.

Function Definition

Here is the complete implementation of varedit:

varedit() { 
    if command -v vipe >/dev/null 2>&1; then 
        if [ "$#" -eq 1 ]; then 
            local var_name="$1" 
            local original_value="${!var_name}" 
            local was_empty=0 
 
            if [ -z "$original_value" ]; then 
                echo "Environment variable '$var_name' does not exist. Creating it as an empty variable." 
                export $var_name="" 
                was_empty=1 
            fi 
 
            # Use vipe to edit the variable 
            new_value=$(echo "$original_value" | vipe) 
 
            if [ "$new_value" != "$original_value" ]; then 
                export $var_name="$new_value" 
                echo "Environment variable '$var_name' has been updated." 
            elif [ "$was_empty" -eq 1 ]; then 
                unset $var_name 
                echo "No changes made. Environment variable '$var_name' has been unset." 
            else 
                echo "No changes made to the environment variable '$var_name'." 
            fi 
        else 
            echo "vipe is installed, but the function requires exactly one parameter." 
            exit 1 
        fi 
    else 
        echo "vipe is not installed." 
        exit 1 
    fi 
}

Step-by-Step Guide to Using varedit

  1. Ensure vipe is Installed
  • Install vipe if it is not already available on your system.
  • On Debian/Ubuntu: sudo apt install moreutils
  • On Red Hat/Fedora: sudo dnf install moreutils
  • On macOS (with Homebrew): brew install moreutils

2. Add the function to bashrc

Add the function to your profile bashrc file, append to the end.

vim ~/.bashrc

Then we need to reload bashrc for changes to take effect

source ~/.bashrc

3. Call the Function Use the function in your terminal or script by providing the name of the environment variable to edit:

varedit VAR_NAME

4. Edit in vipe

  • If the variable exists, its value will be shown in the vipe editor.
  • Modify the value as needed and save/exit.

5. Handle Results

  • If the variable’s value was changed, it will be updated.
  • If no changes were made and the variable was empty initially, it will be unset.

Conclusion

The varedit function is a powerful tool for managing environment variables interactively. It ensures a smooth workflow by integrating with vipe and includes safeguards for handling non-existent variables gracefully. By leveraging varedit, you can efficiently manage environment variables dynamically in your shell environment.