Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions data/start-cosmic
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,25 @@ if command -v systemctl >/dev/null; then
# For environment variables already imported into the user's
# session, if the value imported differs from the value in this
# environment, update it.
existing_env_vars=( $(systemctl --user show-environment | tr '\n' ' ') )
mapfile -t existing_env_vars < <(systemctl --user show-environment)
for env_var in "${existing_env_vars[@]}"; do
env_var_name="$(echo "${env_var}" | awk -F '=' '{print $1}')"
env_var_val_str_to_compare="${env_var_name}=${!env_var_name:-}"
env_var_name="${env_var%%=*}"
env_var_value=${!env_var_name:-}

if [[ "${env_var}" != "${env_var_val_str_to_compare}" ]]; then
# Update only if the value in current environment is non-empty
env_var_unassigned_str="${env_var_name}="
if [[ "${env_var_val_str_to_compare}" != "${env_var_unassigned_str}" ]]; then
systemctl --user import-environment "${env_var_name}" ||:
fi
# Skip current iteration if the environment variable's value
# in the current envionment is unset.
if [[ -z "${env_var_value}" ]]; then
continue
fi

env_var_val_str_to_compare="${env_var_name}=${env_var_value}"
env_var_val_str_to_compare_ansi_c_quoted="${env_var_name}=\$'$(printf '%q' "${env_var_value}")'"
if [[ "${env_var}" == "${env_var_val_str_to_compare}" ]]; then
continue
elif [[ "${env_var}" == "${env_var_val_str_to_compare_ansi_c_quoted}" ]]; then
continue
fi
systemctl --user import-environment "${env_var_name}" ||:
done
fi

Expand Down