Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 481  / 2 Years ago, sun, january 9, 2022, 4:00:33

I have some script in my $HOME/bin folder.
When I access my machine remotely I can't use them and I can't understand
why. They work just fine when I'm in front of my remote computer.



I think it might have something to do with the .bashrc file. The PATH is set with



PATH=$PATH:$HOME/bin
export PATH


it seems correct to me though.



I have also tried to copy the file in the /usr/bin folder logging out and in again,
but still I can't manage to use my script.



If I go in the ~/bin directory and type ./myscript the script work (it is just that I need to run it from different folders where I have some files to process).


More From » ssh

 Answers
4

This is happening because when you log in via SSH, the shell you get is a login shell. In contrast, to use the terminology from the bash documentation, when you open a Terminal window in an already-started graphical login session, the shell you get is still an interactive shell but it is not a login shell.



From man bash:




When bash is invoked as an interactive login shell, or as a
non-interactive shell with the --login option, it first reads and
executes commands from the file /etc/profile, if that file exists.
After reading that file, it looks for ~/.bash_profile,
~/.bash_login, and ~/.profile, in that order, and reads and
executes commands from the first one that exists and is readable. The
--noprofile option may be used when the shell is started to inhibit this behavior.



When a login shell exits, bash reads and executes commands from the
file ~/.bash_logout, if it exists.



When an interactive shell that is not a login shell is started,
bash reads and executes commands from /etc/bash.bashrc and
~/.bashrc, if these files exist. This may be inhibited by using the
--norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc
and ~/.bashrc.




So you should check if you have any of these files:




  • ~/.bash_profile


  • ~/.bash_login


  • ~/.profile




If you do, then you should edit the first one you find and make it add $HOME/bin to your path. If you don't, create one of them for this purpose. (The best one to create is ~/.profile since other shells will use that too.)



You almost certainly have ~/.profile, since Ubuntu's default behavior is to create this with any new user account (including the first account, created when you install Ubuntu).



However, by default this file already contains the necessary lines to add your private bin directory to the $PATH:



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


So it's a bit strange that this is not already working for you.



Depending on your needs, you might want to make ~/.profile (or ~/.bash_login or ~/.bash_profile) call ~/.bashrc:



source ~/.bashrc


But it would be even better to just include lines adding $HOME/bin to your $PATH in ~/.profile (or ~/.bash_login or ~/.bash_profile), but not ~/.bashrc. After all, if you do this for every login shell, it should also happen when you log in graphically, and be inherited by all your interactive shells that aren't login shells.


[#37838] Monday, January 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sconhoney

Total Points: 403
Total Questions: 118
Total Answers: 109

Location: Andorra
Member since Mon, Jan 9, 2023
1 Year ago
;