Where is the default terminal $PATH located on Mac? - macos

I have been looking throughout the system but I cannot find it. When I do echo $PATH I get the stuff I added, plus the default path. I do not have a .profile, and I do have a .bashrc, but the default path is not in there. I am looking for it just to know where it is located because all the tutorials explain that its in .profile... but what if you don't have one? Where is it located then? Anybody have any ideas?

If you do sudo man path_helper, it talks a bit about how it puts the path together. You might look in /etc/paths and /etc/paths.d. I did, and found what I was looking for.

Many system-wide settings including PATH are set in /etc/profile which is read in by bash at startup. On Mac OS X this file usually uses path_helper to set PATH. This utility in turn reads the information from other system configuration files under /etc (see path_helper manpage).
Note that even if you disable the reading of the initialization files by bash (e.g. with command-line options like --noprofile) it will still inherit the environment of the parent process.

If you start at /etc/profile, it should look something like this:
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
It's testing to see if the file exists and if so, executes it. If you execute it by hand, you'll get something like this:
PATH="/usr/bin:/bin:/usr/sbin:/usr/local/bin:/usr/X11/bin"; export PATH;
I believe that's what you're looking for. So it comes from /etc/profile, which in turn calls an executable that sets the path.

As mentioned in the accepted answer, the $PATH is built by first looking into the content of the file /etc/paths, and then looking into every file in the folder /etc/paths.d. So, the $PATH in the pre-installed bash system installation contains every entry in these files, as well as in other shell types.
However, because in the latest Mac OS versions the default shell is zsh, I followed a couple of tutorials in which the writer avoided to change the $PATH for the bash shell, and simply added new entries to the $PATH by editing ~/.zshrc the following way:
export PATH=/path/available/only/for/zsh/shells:$PATH
The above command adds /path/available/only/for/zsh/shells to the $PATH, and the added path will only be available in zsh shells.
I hope this helps someone who, like me, had too many entries in the $PATH in zsh shells, and couldn't figure out where they were coming from!

The .profile file on Mac is located in your user folder: ~/Users/youruser/
However, the .profile file is hidden. You can press Command+shift+. (command, shift, dot) while on Finder to see them.

There's one important fact I only realized today while debugging a problem: the profile settings (.bash_profile etc.) are only read by login shells. They are not read by the processes that are used to launch your applications.
You launch your applications in diverse ways: click the icon in /Applications, or type the name in Spotlight search, or click an icon in the Dock ... In all those cases, the application itself (i.e the binary or shell script inside the application) is launched by launchd without any parent shell. Meaning that your profile is not run and that your custom settings (PATH, environment variables ...) will be ignored.
That can cause all sorts of trouble, for example if you setup you environment to use a specific version of Java: your application will not see that and use the "default" java, which will be the one with the highest version number.
In my case, the problem is that my application was crashing when run via the application launcher, but runs fine when run from a terminal window ... The reason was that I had a custom path that included some libraries required by the application, but that was not available when the application was run by the launcher.
The solution I used was to symlink the libraries needed into /usr/local/lib

Related

OSX differentiate applications with same name in PATH

In OSX, I have added a new path to /etc/paths. It is a path to a medical image viewer application known as rview.
It seems to clash with vim and on typing rview in bash it starts up vim.
But my question is more general, how do you differentiate applications with the same names included in PATH?
The path is evaluated in order; the first matching executable is run.
To edit this, you should change your user profile, not the system-wide configuration. You may have a line similar to this in your ~/.bash_profile file:
export PATH=/usr/local/sbin:/usr/local/bin:$PATH
Just add the path to your application before $PATH and it should work fine. If the line isn't there, create it.
And, if you really want to edit the system paths, just add yours at the top of /etc/paths instead of the bottom.

Setting up ccache in Ubuntu

I am currently following the guide to setting up and compiling AOSP in Ubuntu. The problem is I do not have the best knowledge of Linux/Ubuntu.
The part that currently has me confused is setting up the ccache found at this link https://source.android.com/source/initializing.html.
What I don't really understand is the following, which .bashrc file do I need to edit/add the information to? And can I have more than 1 .bashrc file in Ubuntu? Not really sure what this file really does.
Thanks
.bashrc is a file which is called by bash before on each start of a new shell.
This file can be used to setup the environment, export variables, create aliases and functions. There are usually multiple copy of this file, One per system and one per user to allow system wide configuration but also customization by users ( users bashrc will be called at last and can overwrite things).
here you edid or add line to your won user's .bashrc file
type cd $HOME
vi .bashrc
after that change your file and then you have to compile the file using one of the commands.
..bashrc
or
source .bashrc

mac os x terminal problem faced

oh my god...i faced a big problem...i was created a .bash_profile in ~ folder and then set paths there...bust the big problem is after restarting my bash i see that none of my commands work like LS and RM and etc...
now i dont know how to fix it...some one help me...i need my terminal as soon as possible...
Make sure you are appending to the existing $PATH.
PATH=$PATH:/Users/mthalman/bin
To prevent this happening in the future:
When I edit my environment files (including bashrc, profile, login, and others), I always try starting another shell before quitting my editing environment. This protects me from the possibility of breaking my environment so that I can't log in.
Make sure your PATH includes the usual bin directories: /bin and /usr/bin.
First I would rename ~/.bash_profile to ~/old.bash_profile.
Then open that up in TextEdit (as a plain text document) and verify how you have set your path.
If you would prefer to use vim/emacs/nano/whatever, the act of renaming the file will allow new terminal sessions to use default paths, so from the command line you should be mostly fine.
Then verify you haven't clobbered $PATH as suggested by #Mark Thalman, above.
If you are in a Terminal Window, simply add in the /bin and /usr/bin back in your PATH.
$ PATH="/bin:/usr/bin:$PATH"
That should allow all the basic Unix command to work once more. Or, you can use the full path name for commands:
$ PATH="" #Can't find nothin'
$ ls
bash: ls: command not found.
$ /bin/ls -a #This will work!
. .. .bash_profile foo bar
Don't Reset PATH in your .profile!
As you discovered, you should never reset PATH in your `.bash_profile. Instead, you should always append and prepend to it:
PATH="/usr/local/bin:$PATH"
PATH="$PATH:$HOME/bin"
The first line will prepend /usr/local/bin to PATH which means if a command is in /usr/local/bin and /usr/bin, the /usr/local/bin version will be executed. Many system admins will put alternative base system commands in /usr/local/bin. For example, on Solaris, they might put VIM in /usr/local/bin/vi, so when you edit a file, you're using the improved VIM and not the base VI.
The second line appends your $HOME/bin to the end of $PATH. That means if there's a /bin/ls and you have ~/bin/ls, the /bin/ls will be executed first.
Never set PATH from scratch because each Unix system might have commands that you to access elsewhere in the system. For example, your site might require you to use X11, so you want /usr/X11/bin in your PATH, or you have GIT installed under the /opt/git directory, and you'll need /opt/git/bin in your path.
Sometimes, base utilities like ls might be replaced with upgraded versions of these utilities. On Solaris, you have the base vi and ls command, Most users like the GNU ls command because it uses color and prefer VIM to plain VI. I would included these utilities in /usr/local/bin and prepend that to my PATH.
And now a Word from a Sponsor
As you probably discovered, Finder doesn't list hidden files. That's why you can't see .bash_profile in Finder. You can use some hacks to change this, but it requires you to type them into the terminal window.
I use a Finder replacement called Path Finder. It contains a lot of neat Power User things such as allowing you to see hidden files, treat Packages such as apps as directories, and be able to view protected directories if you have Administrator access. There's a built in terminal and GUI Subversion client.
It's not cheap ($40), but you can download for free and try it out for 30 days.
BTW, I have absolutely no relationship to Cocoatech except as a customer, and I make no money from people buying Path Finder. It's just a tool I use.

Is moving custom shell script to /usr/bin problematic?

Is it dangerous, insecure or not-so-smart for any other reason to put a custom shell script to /usr/bin and add /usr/bin to $PATH in order to make a custom script executable from everywhere without ./ and the file extension?
Bonus question: can I assign custom icons to custom executable scripts?
Traditionally, /usr/bin is one of the places where operating system binaries are stored. For custom scripts, you'd use /usr/local/bin. This you have to create yourself if it does not exist and add to $PATH, as you mentioned.
Icons are a GUI thing, shell scripts are a CLI thing. They live in separate universes. Nothing prevents you from creating a bridge though. For instance, you can make a shell script and call it foo.command. Opening this from the GUI starts Terminal and runs the script. Since you see the file in the Finder, you can assign it a new icon through the Info pane.
Also, you may want to take a look at the free Platypus application. It allows you to create a full-blown application bundle around a script. The bundle will contain the script, so you won't have to put it in some obscure directory and modify $PATH. If you also need CLI access, this option is less desirable.
I wrote a command line tool for setting the custom icon of a file. You can grab it here.

Where do I put mxmlc so that I could just type 'mxmlc' in the terminal to compile a swf file?

I'm on a Mac and I'm trying to make a Vim plugin for compiling/running actionscript files.
First, I need to run mxmlc on the command line, but to do that I have to keep on typing the path to it. Where do I place it so that I don't have to retype the path?
You need to modify your "$PATH" environment variable, so that the tool is in that directory. However, if you want to make this very easy... you can download my macosx-environment-setup.tar.bz2 program. If you execute the "install.sh" script using "sudo ./install.sh", it will setup your environment in such a way that if you use "/Library/Flex4SDK" as the location for the Flex4SDK, it will automatically find it, define FLEX_HOME to point to that location, and it will also ensure that the binaries from the Flex4SDK are in your PATH.
Side Note: This is up on the web, because I use it in my Development Environment Setup How-To Guides. If you aren't too keen about running "sudo ./install.sh", you need to choose a location (I am going to assume "/Library/Flex4SDK", so that the tools are located in "/Library/Flex4SDK/bin"), and then you would simply need to edit your "~/.profile" file (using "nano ~/.profile"), adding the following to the very end:
export FLEX_HOME=/Library/Flex4SDK
export PATH="$PATH":"$FLEX_HOME/bin"
Note that these changes occur in your shell... they will not affect programs that are launched by double-clicking them in Finder. In order to affect those programs, you will need to place the environment variables in a file named ~/.MacOSX/environment.plist. See Automatically build ~/.MacOSX/environment.plist for a script that will automatically generate such a file using the current environment variables defined in your shell.
There are a few ways to answer this:
In one of your directories searched
by PATH (see the list with echo
$PATH)
Add a new directory to PATH
(e.g. in your ~/.bashrc
export PATH=$PATH:/path/to/bindir)
Add an
alias to your program (e.g. in your
~/.bashrc alias
mxmic=/path/to/mxmic)
(I'm assuming you're using bash shell, which is usually the case you can check with echo $SHELL)

Resources