2

I saved a shell script in bin(created by me). When I run the program, to avoid ./ to run the script I need to copy the script to one of the directories specified by PATH variable. The bin directory created is not in the path list. So, to add to the PATH list:

export PATH=~/bin:"$PATH"

But this directory is not getting saved to the PATH list. When I open a new terminal window I need to export it again to run the script. How can I add it to PATH list permanently? Also if I try to move the script to the present PATH listed directory, it says PERMISSION DENIED. Do I have to move it by becoming a root user?

7
  • 3
    After creating ~/bin, you need to log out/in or run source ~/.profile apply. No need to add it to $PATH manually. Other issue might be that the script needs to be executable. Please mention the situation. Jul 6, 2016 at 18:43
  • 'source ~/.profile' is working but after opening new terminal I need to again prompt that. But, I want my script to run directly without mentioning 'source ~/.profile' again.
    – Rohan
    Jul 6, 2016 at 19:22
  • 1
    Please log out/in, and it works for sure. Jul 6, 2016 at 19:29
  • @Rohan can you please provide us with two things. One , contents of your PATH variable. Do echo $PATH and copy it here. Second, contents of your ~/.profile would be nice. Use paste.ubuntu.com and provide the link. Also, what type of system is this ? Is it desktop ? are you using it over SSH ? are you the only user of the system or you have admin who sets up things for you ? Also what version of Ubuntu ? Jul 6, 2016 at 19:51
  • @Serg Here is the paste link. I am using 16.04 LTS , only user and I am not using SSH. The bin file I created is in Home dir and this script does not work without the first command in the paste.
    – Rohan
    Jul 6, 2016 at 19:56

5 Answers 5

3

You can add export PATH=~/bin:"$PATH" to your ~/.profile file. Add it to the bottom, save then reconnect to SSH.

Alternatively, if you want this change to be system wide for any user, you can edit the path in /etc/environment.

5
  • No need to do that in Ubuntu. See comment on the question. I read nothing about SSH in the question. Jul 6, 2016 at 18:43
  • I think he has the program in $HOME/bin though. As far as I know that's not part of the default system or user PATH.
    – AlexV
    Jul 6, 2016 at 18:46
  • Yes it is, after log out/in after the dir was created. For a long time in all Ubuntu versions. Jul 6, 2016 at 18:47
  • 1
    Strange. I'm using pretty much a default ubuntu 14.04 server install and this is my path: root@AV-DESKSRV-01:~/work# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games Edit: I just tested this after creating the bin dir. That's pretty neat. Thanks for teaching me something new. :)
    – AlexV
    Jul 6, 2016 at 18:49
  • The bin created is in Home dir. Every time I open the terminal I need to export path / source ~/.profile. But the doubt is how can I run directly without those?
    – Rohan
    Jul 6, 2016 at 19:28
2

This one worked for me.

Open your .bashrc gedit ~/.bashrc

The text editor is opened.

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Paste this and edit the .bashrc. After closing the text editor, source ~/.bashrc

If you get Permission denied, it means the script is not in executable format. chmod 755 program_name This should do the work. Open the new Terminal and it will work.

1

Add custom home dir path to $PATH

  1. Edit your ~/.profile
  2. Add this line after all $PATH related vars:

    export PATH="$PATH:$HOME/your-home-dir-path"

  3. Save file.

  4. Log out / Restart.

Adding this to your ~/.profile enable this change to terminal and text-mode.

Just in case your custom path is ~/bin or ~/.local/bin !!!

As already said in question comment, these locations are already included in PATH in Ubuntu!

You can just:

  1. Create the directory (e.g., ~/bin).
  2. Copy your script to it.
  3. Change its permitions to be executable with the command:

    chmod +x ~/bin/script.sh

  4. Logout / login. Alternatively, you can execute source ~/.profile

1
  • 1
    Again, not needed in Ubuntu, see comment to the question. Jul 6, 2016 at 18:46
1

As already mentioned, your ~/.profile should already contain something like the following:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

However, it sounds like you are launching an interactive non-login bash shell (i.e. you are not connecting over ssh but launching a terminal from a desktop environment) which does not read ~/.profile and instead ~/.bashrc is read as explained here.

You can modify ~/.bashrc to add the path or read ~/.profile for non-login shells as explained here but as Jacob mentioned if you logout/login this is not required.

To understand the difference between login and non-login shell see this answer.

6
  • Not saying it again, but please note that after log out/in, ~/bin appears in $PATH on (all) Ubuntu (desktop) systems. Jul 6, 2016 at 20:09
  • 1
    What Jacob is trying to say is that OP ( or so OP claims ) has created the folder. When he logs out and logs in, it should be that ~/.profile adds the folder to the source. But I understand what you're saying - OP apparently is not in TTY, but in terminal emulator . Still , his ~/bin should be there at login, because AFAIK Ubuntu Unity sourced ~/.profile . Jul 6, 2016 at 20:12
  • I don't think @Rohan has relogged in but only relaunched the terminal window as per his latest comment ".. it is not working in new terminal"
    – Martin K
    Jul 6, 2016 at 20:45
  • @Martin K this is working, but when I create another script in the same dir and try to run it is denying the permission . Why would that be?
    – Rohan
    Jul 6, 2016 at 21:00
  • @Rohan Is the script you're trying to run executable? chmod +x on the file.
    – Martin K
    Jul 8, 2016 at 13:51
0

When you run

export PATH=~/bin:"$PATH"

you are setting the PATH environment variable and exporting it to the running parent terminals. It's like saying "the new PATH variable is this, spread the word". It works, but once you restart your computer the PATH will go back to the configured one. To make the change permanent, you will have to edit the .profile file on your home folder. It usually has this:

    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
        PATH="$HOME/bin:$PATH"
    fi

which would include the $HOME/bin folder automatically if it exists. But if it doesn't you can just add either the commands above, or:

    export PATH="$PATH:$HOME/bin"

to the file and this folder will be included in the PATH on every session of your user.

You need root permissions to write in the /usr/bin directory, but that's not necessary if you change the .profile file.

2
  • Please read the question and the comments. No need to add ~/bin to $PATH manually. That's the directory OP created. Jul 6, 2016 at 19:05
  • You're right, I just assumed something might be wrong on the .profile file on his system. It should have the code to automatically add the ~/bin folder to PATH, but in case it doesn't he can just add it to the file. The issue will probably be solved by the time he logs out and logs in again! :)
    – IanC
    Jul 6, 2016 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .