Not able to access files in the bin folder after setting PATH variable - linux

I have added HADOOP_INSTALL and it's bin to the PATH variable in my .bash_profile (shown below) and executed it using the command . .bash_profile. I can run the command hadoop version fine but when I close the terminal and run the same command again it gives me error as follows
gsidevas#gsidev-cloudvm ~]$ hadoop version
bash: hadoop: command not found
Current .bash_profile
export HADOOP_INSTALL=/usr/local/hadoop
PATH=$PATH:$HOME/bin:$HADOOP_INSTALL/bin
export PATH
What do I need to do so that this HADOOP_INSTALL and it's bin gets set permanently in my environment?

By default, BASH reads and executes commands in .bash_profile only in login shells. If you're creating a terminal via some X11 or similar software, chances are that terminal is not a login shell by default.
You can achieve this effect for every shell by simply moving the changes you made into your .bashrc file. Please note that this only works properly if your username on the system uses "bash" as its shell and not "sh" since for "sh" the .bashrc file is, by default, ignored.

Related

Can I change $PATH from a machine where I am not root

I have a Red Hat Linux machine where I am not root.
In my home folder, I added this to my .bash_profile and .bashrc:
export PATH=/path/to/my/directory:$PATH
Then, I ran ./.bash_profile and ./.bashrc.
However, $PATH is not updated.
Any idea why this happens?
When you run these files -- like when you conventionally execute any script -- they're executed in a separate shell, and changes to that shell's state (working directory, variables, etc) are lost when that shell exits. If the goal is to change the state of the interactive shell that you're operating in, you need to source them instead.
Syntax is as follows:
source .bashrc # on extended shells such as bash only
. .bashrc # or on any POSIX-compliant shell
The space is critical; ./.bashrc would instead be trying to run .bashrc as an executable, with its own interpreter, whereas . .bashrc is using the . command documented at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_18 to execute the contents of the file in your current interpreter.
You need to source your .bash_profile to get the changes in your current shell.

System Shell Scripts Can't Find my Command in PATH

I put a custom command (shell script) in /usr/local/scripts/.
In order to see commands from /usr/local/scripts, I set the PATH using the following methods:
sudo visudo
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/scripts"
sudo nano /etc/profile
PATH="$PATH:/usr/local/scripts"
export PATH
sudo nano /etc/login.defs
ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/scripts
ENV_PATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/local/scripts
sudo nano /root/.bashrc
PATH="$PATH:/usr/local/scripts"
export PATH
sudo nano /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/scripts"
And this works in most cases, but...
I have two script which, in their code, calls a script from /usr/local/scripts/, and it can't find my script!
The first is /etc/network/if-up.d/sendip, which is run as root when the networking stack is initialized.
And the second is /usr/local/scripts/notif-login which is run as root from pam by /etc/pam.d/sshd:
session optional pam_exec.so /usr/local/scripts/notif-login
If I run both script from my terminal shell, another user, with sudo, without sudo, after su, or login with root, it works properly. But when it is runner by the system (first when networking initialized, and the second via SSH) both failed to run scripts from /usr/local/scripts.
Is there another place where I have to set the path?
Bash/sh will not read /etc/profile for "non-interactive shells", such as the shells from which the scripts you mention run. I'm not sure which distribution you're using, but you should just be able to add it to /etc/environment's PATH definition. Ob Ubuntu, for example:
Change:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
To:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/scripts"
Edit
This should work under Raspbian; it does under Debian. If it doesn't, you might need to just modify the path at the start of one of your scripts. None of the normal startup files will execute for a non-interactive session under Bash. Also try outputting $SHELL and make sure the script is running under the shell you think it is.

run a command on the remote machine after ssh login

I would like to switch to the bash environment after ssh login. Currently I have to type bash every time and then cd to my working directory.
Where can I add some settings so it will run automatically for me.
There's an environment variable SHELL that is set to the current shell. You can set it to your preferred shell by running
$ export SHELL=/path/to/shell
In order to find the path to your preferred shell just run following command
$ whereis bash
Enter the path returned by whereis command as your shell path.
There's a .profile(ls -al) or bash_profile that you can add your setting there. If you can't find such a file then create one using touch .profile. (I did create this file on OS X.)
Open the .profile in order to edit it using whatever text editors that you want
$ vim .profile
Then change the $HOME environment variable in order to change your default home directory path. Enter below line in opened file
export HOME=/your/path
Save the file
:w

Add a command for bash script to terminal

I have studio.sh file in my android-studio/bin folder, which I would like to use as a command in bash (like launching any other normal application).
I read somewhere that adding this line to ~/.profile should work,
export PATH=$PATH:/home/goel/android-studio/bin
But it doesn't work. Whats the correct process?
Add the script folder name to PATH environment variable in ~/bash.rc file
and you can also create alias for you script in ~/bash.rc
and source the /etc/bash.bashrc file, now you can issue your script or alias name in any terminal. Hope this helps.
If you change your PATH in a .profile, you still have to make the shell read the .profile. Starting a new terminal is sometimes not enough (some terminals don't read the .profile), in which case you have to log out and back in.
Is studio.sh executable? Have you tried ./studio.sh inside its containing folder to check whether it runs at all?

Missing gem after changing shell to ksh

my MAC is getting hair-wired after I change console to ksh, and change it back again to /bin/bash
the console prompt is now static bash-3.2 regardless current directory I am in. Meanwhile in .bash_profile I have set # modify console
export PS1="\W > "
rake gem and few others in Ruby are missing, although it was reinstalled again after I run bundle install , but there are subsequent strange issue, e.g. rake command does not hit the right rake file..
Any idea?
Thanks a lot.
The .bash_profile is only run by bash when you first log in. (It only gets run by Terminal if you have Terminal set up to make each shell a "login shell"). If you have commands that you want to get run every time you fire up bash, even if it's not a login shell, you should put them in .bashrc instead. But you can always make a shell be a login shell by running bash --login instead of just bash.
Not sure where your ksh comes from, but note that it doesn't understand '\W' etc in the prompt string, so I would expect you to get a literal '\W' in the prompt instead of the expanded working dir. If you're running ksh as a login shell, it's probably something in the .profile (or /etc/profile, etc.). ksh doesn't have an exact equivalent of .bashrc, but if $ENV is set to a filename after the profile runs, that file is executed as well (even on non-login shells, if ENV is already set when the shell starts). Ksh, of course, ignores .bash_profile and .bashrc.
Since there's no ref to gems in the body of your question (anymore?), you might want to change the question and remove the Ruby tag...

Resources