How do programs add to $PATH variable with using .bash_profile? - bash

How do some programs seem to add to the $PATH variable without using a .bash_profile file? What are the advantages to these approaches of adding to $PATH variable, but not using a .bash_profile file?
For my mac
echo $PATH returns
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin
which java returns
/usr/bin/java
which tex returns
/Library/TeX/texbin/tex
How is Java available in the terminal without being in the $PATH variable? (This is an example of something running in the terminal without being in the $PATH variable.)
How did TeX add to the PATH variable without using a .bash_profile file?
What added Library/Apple/usr/bin to the PATH variable? Wikipedia says that only the first three are defaults: /bin, /usr/bin, and /usr/local/bin.
Note: Other StackOverflow posts helped users create a .bash_profile file (a file that doesn't exist by default on a Mac) to run programs such as android and adb when they received -bash: android: command not found
My question is about how the terminal works (like how java is running without being on $PATH) and how other programs (like TeX) added to $PATH without using a file like .bash_profile.
I am running macOS 10.15.5, confirmed that I do not have a .bash_profile file, do have homebrew installed (not sure if that affects anything) and when I open a terminal, it says "The default interactive shell is now zsh." (I think I installed zsh after reading a different StackOverflow a few weeks back.

Related

How to set the $PATH for kitty terminal MacOS?

I've been using iTerm2 on my Macbook Pro, but now I`ve just installed Kitty. After writing a few configurations into the Kitty config file, I noticed that, I can't use homebrew (zsh: command not found: brew) but however with iTerm2 I can use it as usual. I recognized that the $path variable is different for the Kitty terminal and the homebrew bin files folder doesn't appear, but if I check the $path with iTerm2, I can see the homebrew path into the $Path. In addition, the aliases I've got defined into the .zshrc file also don't work with Kitty but yes with iTerm2.
So my question is, why do Kitties have a different $path ? and what must I do to Kitty take the correct $path ?
After restart the kitty.config file the problem solved, so I reviewed this config file and the problem seems to be the shell directive, which was set to zsh. I don't know if we have to specify the absolute path to the zsh shell or whatever, but i deleted this directive and then kitty takes the default shell (zsh in my case) and the problem has been solved.

permanently source in terminal (Mac OS)

this is probably a very basic question. I am using the bash shell still (Catalina). And I downloaded gromacs. Every time I open a new terminal window I first have to put
source /usr/local/gromacs/bin/GMXRC
in first, otherwise I will just get command not found.
How can I permanently source this so that I don't have to reenter it all the time?
I tried editing the /etc/paths file but that didn't work.
If you are still using the bash shell on macOS, all you have to do is:
Put the line:
source /usr/local/gromacs/bin/GMXRC
at the end in .bash_profile.
.bash_profile is in home directory, so you can open it like:
open ~/.bash_profile
Why .bash_profile?
.bash_profile is executed whenever a login interactive shell starts and on mac, every interactive shell is a login shell by default.
If in case you switch to zsh, instead of .bash_profile, I think .zlogin should be used.
But that would depend entirely on your need. Read about zsh configuration files in case you ever need that.

Messed up PATH environment variable in Ubuntu 16.04 [duplicate]

This question already has an answer here:
How to restore .bash_profile on a mac? None of my unix terminal are working [closed]
(1 answer)
Closed 5 years ago.
I tried installing Anaconda to get many python packages at once but had some issues with python IDLE where it said No package found so had to manually set the path in ~/.bashrc.
Once I set the path in ~/.bashrc the IMPORT ERROR in python IDLE was solved but I'm unable to use commands on terminal now.
I'm getting this error all the time.
sid#sids-ubuntu:~$ ls
Command 'ls' is available in '/bin/ls'
The command could not be located because '/bin' is not included in the PATH environment variable.
ls: command not found
sid#sids-ubuntu:~$ sudo
Command 'sudo' is available in '/usr/bin/sudo'
The command could not be located because '/usr/bin' is not included in the PATH environment variable.
sudo: command not found
sid#sids-ubuntu:~$ mkdir aa
Command 'mkdir' is available in '/bin/mkdir'
The command could not be located because '/bin' is not included in the PATH environment variable.
mkdir: command not found
I did export PATH=/usr/bin:/bin to find out my $PATH and content of /etc/environment. It seems both are different.
sid#sids-ubuntu:~$ export PATH=/usr/bin:/bin
sid#sids-ubuntu:~$ echo $PATH
/usr/bin:/bin
sid#sids-ubuntu:~$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
Doesn't Ubuntu look for $PATH in /etc/environment?
If yes, what could be the reason my $PATH is different from /etc/environment? and please help me fix it!
If not, where does Ubuntu look for $PATH? please help me fix it!
Be aware that your /etc/environment is only re-read at reboot.
When you want to change your path, be sure to include the existing part as well. To do that, add $PATH in the new path definition.
export PATH="$PATH:/usr/bin"
Looking at your problems, adding the $PATH in your ~/.bashrc should do the trick. If not, open a new terminal and show us the output of
echo $PATH
When adding some directory to PATH it's good idea not to overwrite previous value, just append desired directory (e.g. $HOME/bin), in your ~/.bashrc add at the end line (and remove any previous tampering with PATH)
export PATH="$PATH:$HOME/bin"
and run:
source ~/.bashrc
(or just open new session of terminal).
PATH is an environment variable, and therefore it is not looked up in any file.
There are several files which are sourced when bash is invoked (see the section named INVOCATION in the bash man page), and while sourcing these files, the environment variable PATH can be set, respectively manipulated. Note that .bashrc is not always processed; please read the bash man-page carefully to understand, which files are included under which condition.

Unix commands through Matlab - PATH not right?

I've installed a series of binaries which appear in usr/local/bin on my MACOSX (They're called DCMTK). The usr/local/bin folder appears on the path as expected:
PATH=/Users/jim/Library/Enthought/Canopy_64bit/User/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin:niftyreg_install/bin
I can run these commands as expected from any folder in a Terminal window, however I can't seem to run any of them from within Matlab (2014b) using the command:
cmd=['dcmdump -h'];
system(cmd)
/bin/bash: dcmdump: command not found
Running 'env' in a Terminal shows that I am using the same SHELL as Matlab:
SHELL=/bin/bash
My question is why is it when Matlab invokes bin/bash it is not finding the binaries in usr/local/bin? Is there something in .bash_profile I need to update?
Thanks,
Jim
As Mark Setchell already pointed out in his comments, you can echo the PATH variable from within MATLAB using
cmd=['echo $PATH'];
system(cmd);
You can also get environment variables, such as $PATH using the MATLAB getenv function:
getenv('PATH');
As you also posted in comments, in your case /usr/bin/local is missing in the $PATH variable. MATLAB has an option to set environment variables via the setenv function. But watch out: This sets the variable to only the specified value. To append a folder, you have to query the existing variable and append a folder to that:
setenv('PATH', [getenv('PATH'),':','/usr/bin/local']);
As separator, either ; (for Windows systems) or : (for Unix based systems) is used. In the example above I added : as you are working with Mac OS X.

Setting environment variables in Yosemite

What is the proper way to modify environment variables like PATH in Yosemite?
This is this question Setting environment variables in OS X? but specifically for yosemite since it doesn't work anymore.
Have you tried editing ~/.bash_profile?
Adding a line like this to bash_profile ought to do it:
export PATH=/usr/local/bin:$PATH
What shell are you using? I'm assuming you're using the default Bash shell. There's also Csh, Ksh, and Zsh.
The Terminal.app on Mac OS X by default starts a new login shell each time a window is open. This means that $HOME/.bash_profile or $HOME/profile is always executed when you open a new terminal window. You can set particular defaults in here. For example, I set PS1 and set -o vi.
NOTE: This may not be the case if you're using other Terminal apps like xterm. These open new terminal windows as just new shells. This means that you may not see the changes made in .bash_profile until you log out and log back in.
You can try editing $HOME/.bashrc and see if that helps.
What about other shells?
If you're using Kornshell (ksh), you need to edit the $HOME/profile and not $HOME/.bash_profile. If you're using Zshell (zsh), you're on your own. It's too wacky to describe here. Read the manpage for zsh and search for ZDOTDIR.
When you run a shell script, the $HOME/.bashrc is executed. Most people put something like this in their .bash_profile, so their .bashrc settings are included in a new terminal window:
[[ -x $HOME/.bashrc ]] && source "$HOME/.bashrc"
Some people set things they want to be set when they run a shell script, for example export $PS4="\$LINENO> ".
The $PATH is a bit different. You can set it in .bash_profile (I would not set it in .bashrc), But, Mac OS X has an automated why on how systemwide paths are set. A file called /etc/paths is used to set the default path for all users using either Bash or Kornshell via the /usr/libexec/path_helper program.
On my Mac, I set my $PATH to:
/usr/local/bin:/usr/share/bin:/bin:/usr/bin:/usr/sbin:/sbin:$HOME/bin
When I install programs, I usually install them under /opt when possible. Then, I link their binaries (where ever they're placed) to /usr/local/bin. This way, I don't have to keep building my PATH. Plus, it allows me to override system defaults. For example, /usr/bin/git is at 1.9.3. while my installed /usr/local/bin/git is at version 2.2.1.
One thing you should not do is modify /etc/profile because changes there may be replaced on OS X upgrades.
The problem is not with environment variables set and accessed from within /bin/bash or /bin/sh, but with envars that should be set for programs NOT executed from the shell; i.e. normal apps executed from the dock or Finder.
After finally getting things right with launchctl in Mavericks, Apple is in the process of changing things again. The useful subcommands of launchctl are now labelled as "legacy subcommands", some of which are no longer supported. That leaves a question mark over the others.
In any case, the most important subcommands are still available for now.
launchctl allows for the setting of the overall environment in which user processes execute. It appears that the overall user environment is inherited by all Terminal processes; i.e. all setenv variables are exported. It's a bit tricky to confirm that. In any case, you will still need your .profile and .bashrc to define functions and aliases, which are not supported by launchctl.
I go to some lengths to ensure that all of my launchctl vars are also defined in my profile. This enables me to set up the same environment on remote or VM linux systems, with a few minor tweaks.
Al of my setup is described in this blog post .
following solution worked for me.
Open Terminal
Click on Terminal Menu at right upper corner.
click on Preferences
Click on General
Change Shell open with to command and put /bin/bash in text box.
Now whatever configuration you do in ~/.bash_profile takes effect. Previously you were not using bash(were using ksh) that is why it was not reading .bash_profile.

Resources