Setting environment variables with .bash_profile: only last export works properly

5,807

I see in my crystal ball that you're editing the file on a Windows machine.

Windows and Linux have different ways of representing line breaks. On Linux (like any unix system), a line consists of a series of characters which ends with a line feed (LF = \n = Ctrl+J) character. On Windows, lines are separated by a two-character sequence: carriage return, then line feed (CRLF = \r\n = Ctrl+M Ctrl+J).

Your file contains the export PATH=… command, then CR, LF, and the export LD_LIBRARY_PATH command with no newline at the end. For Linux programs, the same sequence of bytes represents export PATH=… with a CR character at the end, then a line break, then an unfinished line containing export PATH=…. Your shell faithfully executes the first command, which adds a directory to PATH with a name ending in a CR character. Since there is no directory by that name, the change of PATH has no effect. Then the shell treats the partial line as if it was a complete line, which is the intended command to set LD_LIBRARY_PATH.

When you appended the directory the second time, you didn't type a CR character into the command, so the command worked. When you displayed the output, the CR character was interpreted by the terminal to mean “move back to the beginning of the line”. Since you had exactly the same content before and after the CR character, that appeared to print that content once — in fact it was printed twice at the same position.

Edit your Linux configuration files under Linux, or tell your Windows editor to use Linux line endings (most text editors that are less brain-dead than Notepad can do that).

Share:
5,807

Related videos on Youtube

gsmafra
Author by

gsmafra

Updated on September 18, 2022

Comments

  • gsmafra
    gsmafra over 1 year

    I am trying to export some environment variables in Unix using .bash_profile

    It reads now like this:

    export PATH=${PATH}:/usr/local/cuda-6.0/bin/
    export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda-6.0/targets/x86_64-linux/lib/
    

    After that I try to run a python script that uses these variables

    python
    >>> import theano
    

    And I get this error:

    ERROR (theano.sandbox.cuda): nvcc compiler not found on $PATH. Check your nvcc installation and try again.
    

    Then I exit python, re-run manually the first line of .bash_profile and it works.

    If I change the order of the exports like this:

    export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda-6.0/targets/x86_64-linux/lib/
    export PATH=${PATH}:/usr/local/cuda-6.0/bin/
    

    And try one more time to import theano from python I get this error:

    ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: libcublas.so.6.0: cannot open shared object file: No such file or directory
    WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available  (error: cuda unavilable)
    

    Which indicates that export LD_LIBRARY_PATH didn't work. I do the same procedure as before, exit python re-run the first line and then it imports correctly.

    Adding the first export to the last line of the bash script (exporting twice) makes this first/third export work well but not the second, that is, only the last export is working properly.

    I've tried doing echo commmands on the variables and it was printing normally as if they were correctly set, but I'm unable to use them with python. Also, when re-setting them manually that doesn't double export them, but re-re-setting does:

    -bash-4.2$ echo $LD_LIBRARY_PATH
    :/usr/local/cuda-6.0/targets/x86_64-linux/lib/
    -bash-4.2$ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda-6.0/targets/x86_64-linux/lib/
    -bash-4.2$ echo $LD_LIBRARY_PATH
    :/usr/local/cuda-6.0/targets/x86_64-linux/lib/
    -bash-4.2$ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda-6.0/targets/x86_64-linux/lib/
    -bash-4.2$ echo $LD_LIBRARY_PATH
    :/usr/local/cuda-6.0/targets/x86_64-linux/lib/:/usr/local/cuda-6.0/targets/x86_64-linux/lib/
    

    I've actually solved this issue by re-setting the PATH variable inside the python script, but it bothers me that I can't find the reason why this is happening

    Some info:

    -bash-4.2$ bash --version
    GNU bash, version 4.2.53(1)-release (x86_64-redhat-linux-gnu)
    -bash-4.2$ cat /etc/*-release
    Fedora release 19 (Schrödinger’s Cat)
    
  • gsmafra
    gsmafra almost 9 years
    I've tried this, it is reading ~/.bash_profile and not ~/.bash_rc
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    And are you logging out and logging in again every time you modify your .bash_profile?
  • gsmafra
    gsmafra almost 9 years
    Yes. (15 characters)
  • gsmafra
    gsmafra almost 9 years
    I'll have to wait until Monday to confirm this, but it makes perfect sense. Thank you. And yes, I'm using a SSH/SFTP client to run Linux remotely from Windows