2

I need to add path to my executable application to PATH environment variable. Because I need to add path during .deb package installation, I should use postinst script in debian package. After reading Ubuntu official docs and Askubuntu question I decided to use /etc/environment file to add my application path due to system wide user access. Here is my bash script:

#! /bin/bash

cd ~
echo 'PATH="$PATH:/path/to/my/bin"' >> /etc/environment

and here is /etc/environment content after executing postinst bash script:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
PATH="$PATH:/path/to/my/bin"

Now when I log out and try log in, I stuck in login loop!

Update 1:

I tested the approach for .profile in Home directory and it works fine! But I need to add the path for all users so I must use /etc/environment file.

  • 1
    You should symlink the executable to /usr/bin instead of depending of $PATH. Anyway, here is the answer: Place an executable script in /etc/profile.d that set $PATH – leorize Jul 24 '15 at 11:23
  • @Archuser I tested the approach for .profile in Home directory and it works fine! But I need to add the path for all users. – Seyed Morteza Mousavi Jul 24 '15 at 11:27
  • @SeyedMortezaMousavi as Arch user said, place an executable script in /etc/profile.d/ with .sh extension (which is what you should do if you're doing this from a package - or how will you reliably undo the addition on package removal?) – muru Jul 24 '15 at 11:29
  • @SeyedMortezaMousavi scripts in /etc/profile.d are executed for all users – leorize Jul 24 '15 at 11:49
  • @Archuser thanks for your answers. It works now. If you add your answers I can select it as answer. – Seyed Morteza Mousavi Jul 24 '15 at 11:50
  • Just wanted to mention that such a file should not be installed via postinst, but it should be installed as a package file the normal way - probably via debian/install. Or, if you go for a symlink instead, via debian/links. – Gunnar Hjalmarsson Jul 24 '15 at 12:22

1 Answers1

6

Please avoid modifing system files. Instead you should place an executable script in /etc/profile.d (scripts in here got executed for every user) to change $PATH value.

/etc/profile.d/10-<package name>.sh

#!/bin/sh
export PATH=$PATH:/path/to/executable
leorize
  • 811