spock
5/13/2017 - 3:43 PM

Script to permanently set environment variables under Linux (Ubuntu 16.04 and later). Run as sudo.

Script to permanently set environment variables under Linux (Ubuntu 16.04 and later). Run as sudo.

Usage: sudo set_env_var.sh key value

#!/bin/bash
# run under sudo
# script for permanently setting environment variables, found here:
# https://stackoverflow.com/questions/13046624/how-to-permanently-export-a-variable-in-linux

add_env_var()
{
  KEY=$1
  VALUE=$2
  echo "export "$KEY"="$VALUE>>~/.bashrc
  echo $KEY"="$VALUE>>~/.profile
  echo $KEY"="$VALUE>>/etc/environment
  source ~/.bashrc
  source ~/.profile
}

if [ "$(id -u)" != "0" ]; then
	echo "Sorry, you are not sudo."
	exit 1
fi

add_env_var() $1 $2

exit