Here's a more flexible way to achieve the desired:
Insert or update environment variable export
rcFile="~/.bashrc"
prop="WAYLAND_DISPLAY" # export property to insert
val="wayland-1" # the desired value
if grep -q "^export $prop=" "$rcFile"; then
sed -i "s/^export $prop=.*$/export $prop=$val/" "$rcFile" &&
echo "[updated] export $prop=$val"
else
echo -e "export $prop=$val" >> "$rcFile"
echo "[inserted] export $prop=$val"
fi
Delete environment variable export
rcFile="~/.bashrc"
prop="POSTGRE_PORT" # export property to delete
if grep -q "^export $prop=" "$rcFile"; then
sed -i "/^export $prop=.*$/d" "$rcFile" &&
echo "[deleted] export $prop"
else
echo "[not found] export $prop"
fi
For an implementation idea see: gh.sh