Score:0

Add permanently environmental variable to Ubuntu

qa flag

I work with Ubuntu 18.04, and using softwares like: petalinux, Vivado. Each time i want to open vivado for example, I open terminal and type:

source /opt/XIlinx/Vivado/2020.2/settings64.sh

This is applied only to that specific terminal.

I understand that if I edit .profile file, I can make it redundant. So i add this line:

export PATH="$PATH:/opt/XIlinx/Vivado/2020.2/settings64.sh"

Log out, log in, but I still can't open vivado without sourcing settings64.sh file. What am I doing wrong?

this is the petalinux source file - settings.sh:

#
# Setup script for PetaLinux development environment.
#
# Copyright (c) 1995-2013 Xilinx, Inc.  All rights reserved.
#

SETTINGS_FILE='settings.sh'
# The right location will be replaced by the installer
XIL_SCRIPT_LOC="./"

if [ $# != 0 ]; then
    # The first argument is the location of PetaLinux Installation
    # Don't detect the installation location
    XIL_SCRIPT_LOC="$1"
else
    #  XIL_SCRIPT_LOC should point to script location
    if [ "$0" == "ksh" ]; then
        XIL_SCRIPT_LOC_TMP_UNI=`readlink -f ${XIL_ARG_}`
    else
        XIL_SCRIPT_LOC_TMP_UNI=$BASH_SOURCE
    fi
    XIL_SCRIPT_LOC_TMP_UNI=${XIL_SCRIPT_LOC_TMP_UNI%/*}
    if [ "$XIL_SCRIPT_LOC_TMP_UNI" != "" ]; then
        if [ "$XIL_SCRIPT_LOC_TMP_UNI" == "settings.sh" ]; then
            XIL_SCRIPT_LOC_TMP_UNI="./"
        fi
        XIL_SCRIPT_LOC_TMP_UNI=`readlink -f ${XIL_SCRIPT_LOC_TMP_UNI}`
        if [ $? == 0 ]; then
            XIL_SCRIPT_LOC=${XIL_SCRIPT_LOC_TMP_UNI}
        fi
    fi
    unset XIL_SCRIPT_LOC_TMP_UNI
fi

export PETALINUX=`readlink -f "${XIL_SCRIPT_LOC}"`

if echo "${PETALINUX}" | grep -q ' '; then
    echo "********************************************************"
    echo "WARNING: PetaLinux SDK installation path contains spaces"
    echo "WARNING: You are STRONGLY recommend to fix this".
    echo "********************************************************"
fi
export PETALINUX_VER=2020.2

export XSCT_TOOLCHAIN="${PETALINUX}/tools/xsct"

# Figure out host system architecture
# for now, only linux-i386 supported

#
# Add toolchains to user's search path
#
PATH="${XSCT_TOOLCHAIN}/gnu/aarch32/lin/gcc-arm-none-eabi/bin:${PATH}"
PATH="${XSCT_TOOLCHAIN}/gnu/aarch64/lin/aarch64-none/bin:${PATH}"
PATH="${XSCT_TOOLCHAIN}/gnu/armr5/lin/gcc-arm-none-eabi/bin:${PATH}"
PATH="${XSCT_TOOLCHAIN}/gnu/microblaze/lin/bin:${PATH}"

#
# Add required binary tools to the user's search path
#
PATH="${PETALINUX}/tools/xsct/petalinux/bin:${PETALINUX}/tools/common/petalinux/bin:${PATH}"

#
# Check for "." or ".\" in the path - it's broken
#
echo "${PATH}" | tr ":" "\n" | grep '^\./*$' > /dev/null && 
    echo "WARNING: '.' detected in PATH - fixing it." 1>&2
PATH=`echo ${PATH} | tr ":" "\n" | grep -v '^\./*$' | tr "\n" ":"`

# Strip any trailing or multi-colons - they are interpreted as '.'
PATH=$(echo ${PATH} | sed -e 's/:*$//g' -e 's/::*/:/g')

export PATH


echo PetaLinux environment set to \'${PETALINUX}\'

for s in /bin/sh sh; do
    if ! $s --version 2>/dev/null | grep -q "^GNU bash"; then
        echo "WARNING: $s is not bash! "
        echo "bash is PetaLinux recommended shell. Please set your default shell to bash."
        break
    fi
done

if ! echo $SHELL | grep -q "bash"; then
    echo "WARNING: $SHELL is not bash! "
    echo "/bin/bash is Petalinux recommended SHELL variable. Please set your SHELL variable to /bin/bash."
fi

"${PETALINUX}"/tools/common/petalinux//utils/petalinux-env-check
Raffa avatar
jp flag
Don't add it to your path variable ... source it like `source /opt/XIlinx/Vivado/2020.2/settings64.sh` on a new line.
Raffa avatar
jp flag
Assuming that file contains a bunch of variables/functions to be `export`ed or otherwise that file's contents might not affect shells opened afterwords.
Raffa avatar
jp flag
Reading your question again "*This is applied only to that specific terminal.*" tells me it's not the type that works in `.profile` ... You can add the contents of that `settings64.sh` file to your question instead of us guessing.
guiverc avatar
cn flag
[Ubuntu 18.04 LTS has reached the end of it's *standard* support life](https://fridge.ubuntu.com/2023/05/13/extended-security-maintenance-for-ubuntu-18-04-bionic-beaver-begins-31-may-2023/) thus is now off-topic here unless your question is specific to helping you move to a *fully supported* release of Ubuntu. Ubuntu 18.04 ESM support is available, but not on-topic here, see https://askubuntu.com/help/on-topic See also https://ubuntu.com//blog/18-04-end-of-standard-support
Score:3
cn flag

What am I doing wong

You are defining the path name of an executable script as a directory path. /opt/XIlinx/Vivado/2020.2/settings64.sh is not the path of a directory, it is a path of a script, settings64.sh. That script must be sourced (i.e., each command must be executed in the current shell) for creating the settings your application requires. You know that because you know the command source /opt/XIlinx/Vivado/2020.2/settings64.sh works.

So to have that command executed automatically, it suffices to add the file to the ~/.bashrc file. This file is automatically read and executed whenever you open a terminal. That means the settings are available for any application launched from that terminal.

To have these settings available as soon as you log in, add that line instead to ~/.profile. That file is automatically read and executed whenever you log into the system. That means the settings are available for any application, also these you start using the menu system.

These settings are specific to the programs you run. So setting them universally does not benefit any other aspect of your system. So my preferred approach would instead be to have a wrapper script for that program, so that each time that script is run, proper settings are set and the application is started.

Yonatan Elizarov avatar
qa flag
I see, but I don't understand the difference between .bashrc and .profile. >>To have these settings available as soon as you log in - why i need this?
vanadium avatar
cn flag
I added one line of explanation for each of the cases which will make it clear. Please accept the answer if it resolves the question on how to permanently add environental variables: click the checkmark next to the answer. That shows other users that a suitable answer can be found here.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.