0

This might be a repeated question, but I couldn't relate to any previous answer. I basically want to run my shell-script from anywhere in ubuntu 16.04 (whether I'm inside the directory where the shell-script is placed or not).

I know the first method which is to place the shell-script inside the /home/user/bin directory (that is already set in the $PATH), change the shell-script permission mode and, finally, reset the bash by re-starting the terminal. This way everything worked fine.

But, since I want to learn how to set the environment myself, instead of using /home/user/bin I would like to use my own created directory.

So, I placed my shell-script inside a newly created /home/workspace/myproject/bin directory and, of course, I changed its mode. Then run the following command:

echo $PATH

Then, amended the $PATH variable as follow:

PATH="/echo/command/result:/home/workspace/myproject/bin"

Then, set the environment:

source /etc/environment && export PATH

But it doesn't work !

UPDATE

I know I could also do it through editing the ~/.profile file. But my aim is to do it directly from the terminal without having to open and edit a file. And you can see why, using only two commands in a row, immediately after finishing writing the shell-script, is much faster, right ?

To be clear: I want to know why resetting of the /etc/environment didn't work?

McLan
  • 251
  • 1
  • 7
  • 16
  • 1
    Your PATH statement is incorrect, and /etc/environment resets the path. Please see my answer. Please remember to accept it if it was helpful. Thanks! – heynnema Feb 13 '19 at 18:04
  • Thanks for your reply, but why my PATH statement is incorrect ?! – McLan Feb 14 '19 at 13:57
  • 1
    When sourcing /etc/environment, it negates any path changes that you made, because it has an implicit PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games". Much easier to edit ~/.profile once, than typing this in multiple times, as per my answer. – heynnema Feb 14 '19 at 14:13
  • @heynnema : So basically I can't change that implicit PATH, can I ? [by the way, I want to learn how to do, not only necessary because it is easier] – McLan Feb 14 '19 at 14:23
  • 1
    Then use just the PATH statement from my answer, nothing else. – heynnema Feb 14 '19 at 14:24

1 Answers1

1

Add this to the end of your ~/.profile

It checks to see if the directory exists, and if it does, then it adds it to the existing path.

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

then log out, and log back in.

heynnema
  • 70,711
  • But I don't want to go inside the ~/.profile and edit it. Instead, I want to be able to do it from the command line the way I mentioned in my question (resetting the `/etc/environemnt'. However, your answer is useful, even though it doesn't exactly fit what I am looking for. But, since it was the reason why I updated the question, I'll be very happy to give a thumbs up. – McLan Feb 14 '19 at 14:19
  • 1
    @McLan then just use the PATH statement shown above, nothing else. – heynnema Feb 14 '19 at 14:21
  • Well, that PATH statement partially worked. As once the current terminal is closed, the same command needs to be executed again in the new terminal. Again, even if I put it in the ~/.profile, I need to source the file everytime i open a new terminal. I need a permenant solution whether I restart the terminal or reboot the machine ! – McLan Feb 14 '19 at 14:42
  • 2
    @McLan Yes, the PATH command is temporary, but you asked how to do it manually via CLI. Either you edit ~/.profile, or /etc/environment... but changes to /etc/environment effect ALL users, not just yours. And you don't need to source ~/.profile, cause it gets executed at login time. That's the correct permanent solution for you. – heynnema Feb 14 '19 at 14:46
  • 1
    @McLan as I mentioned earlier, it's just easier to drop your project script into ~/bin, and be done with it. That path already exists. That's what it's for. – heynnema Feb 14 '19 at 14:58
  • Even though it's always good to learn the other ways, I guess I'm going to stick with ~/bin. But THANK YOU so much. I really appreciate your answer and your comments. Have a great day/night :) – McLan Feb 14 '19 at 15:18