Joining array into $PATH in bash on OSX - path

Trying to customize the $PATH env variable on OSX with the following in .profile:
PATH=(
$HOME/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin/
/sbin
)
PATH=$(IFS=:; echo "${PATH[*]}")
export PATH
When this is loaded, I verified the path by doing echo $PATH and the output looks correct:
echo $PATH
/Users/apple/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
However, it doesn't look like any of the above path works.
ls
- bash: (something like not able to find command ls, which is in /usr/bin)
What am I missing here?

Change PATH array variable name to something different, like:
P=(
$HOME/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin/
/sbin
)
PATH=$(IFS=:; echo "${P[*]}")
export PATH
I'm not sure why, though. If I figure that out, I'll update this answer.
Update: for a little bit more info on this, see this topic.

Related

How to see the PATH inside a shell without opening a shell

Use the command flag looked like a solution but it doesn't work
Inside the following shell:
nix shell github:nixos/nixpkgs/nixpkgs-unstable#hello
the path contain a directory with an executable hello
I've tried this:
nix shell github:nixos/nixpkgs/nixpkgs-unstable#hello --command echo $PATH
I can't see the hello executable
My eyes are not the problem.
diff <( echo $PATH ) <( nix shell github:nixos/nixpkgs/nixpkgs-unstable#hello --command echo $PATH)
It see no difference. It means that the printed path doesn't not contains hello.
Why?
The printed path does not contain hello because if your starting PATH was /nix/var/nix/profiles/default/bin:/run/current-system/sw/bin, then you just ran:
nix shell 'github:nixos/nixpkgs/nixpkgs-unstable#hello' --command \
echo /nix/var/nix/profiles/default/bin:/run/current-system/sw/bin
That is to say, you passed your original path as an argument to the nix shell command, instead of passing it a reference to a variable for it to expand later.
The easiest way to accomplish what you're looking for is:
nix shell 'github:nixos/nixpkgs/nixpkgs-unstable#hello' --command \
sh -c 'echo "$PATH"'
The single quotes prevent your shell from expanding $PATH before a copy of sh invoked by nix is started.
Of course, if you really don't want to start any kind of child shell, then you can run a non-shell tool to print environment variables:
nix shell 'github:nixos/nixpkgs/nixpkgs-unstable#hello' --command \
env | grep '^PATH='

Nix shell #! to refer to to shell.nix in a parent directory

I start almost all my scripts lately with
#!/usr/bin/env nix-shell
#! nix-shell -i bash
set -e
But this requires that the default.nix/shell.nix be in the same directory as the script which is not always the case. Is there a way to tell nix-shell to look at all parent directories for a *.nix file?
The items after #! nix-shell are basically regular nix-shell arguments, so you can add the file name like this.
#!/usr/bin/env nix-shell
#! nix-shell -i bash ../shell.nix
set -e
Path resolution happens relative to the script file.
When you use a directory path, it will look for default.nix, not shell.nix inside the directory.

brew doctor Warning: /usr/bin occurs before /usr/local/bin

When I run brew doctor, I get the following error:
Error: /usr/bin occurs before /usr/local/bin
This means that system-provided programs will be used instead of those
provided by Homebrew. The following tools exist at both paths.
How to fix it?
Thanks
Easy way to fix it
sudo nano /etc/paths
And change all lines like this
/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
And than check PATH values on new terminal tab
env|grep PATH
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
Restart your terminal and run
brew doctor
Everythings OK.

Add folder to sudo path without -i

There has been a lot of talk already done on Stack Overflow about adding a folder to the sudo path. But, none of the other tutorials I've seen have really answered the following question:
How can I add a folder to the sudo PATH without using -i.
Here is my setup. The folder I want to add is "/var/folder". There is the bash script "/var/folder/script.sh". I added the following lines of code to the /root/.bashrc file:
if [ -d /var/folder ]; then
PATH=/var/folder:$PATH
fi
Now, when I type in the command "sudo echo $PATH" I get the following output:
/var/folder:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
The problem is, when I run the command "sudo script.sh", the script can't seem to be found. The output is as follows:
sudo: script.sh: command not found
This is in spite of the fact that tab-auto-complete works on "sudo script.sh".
All though the $PATH is defined when you do an echo of it, for running the script it is not actually defined. So to see what is happening, you can run the following:
sudo -s
echo $PATH
You will notice that it will be:
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Where is it getting the $PATH from?
It is defined in your sudoers file:
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Solution
You can update the Defaults secure_path in the sudoers file to have the correct value.
Defaults secure_path="/var/folder:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Going back to how you were seeing the correct value when you ran:
sudo echo $PATH
Since you had $PATH defined with /var/folder before you ran that command, it just replaced the $PATH with the value, but your actual path for sudo was
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
so you were effectively running
sudo echo /var/folder:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

PHP CLI - something like $PATH

I want to ask you if there is something like UNIX $PATH for PHP CLI.
Eg., I want to use
php a2addvhost.php example.com
instead of
php /usr/share/php/a2addvhost.php example.com
I tried to change include_path and $PATH but either work.
If you're doing
php a2addvhost.php example.com
You're still in Unix. So the a2addvhost.php file must be in the current directory for it to work.
The first argument must be the exact pathname. However, you can make a start script (as root):
$ echo -e '#!/bin/sh\nexec php /usr/share/php/a2addvhost.php "$#"\n' \
> /usr/bin/a2addvhost
$ # And then start with ...
$ a2addvhost example.com
Alternatively, make a2addvhost.php executable by prepending it as follows:
#!/usr/bin/env php
<?php
/* php code goes here */
and making it executable:
$ chmod a+x /usr/share/php/a2addvhost.php
Now, if PATH contains /usr/share/php/, you can start your script with
$ /usr/share/php/a2addvhost.php example.com
Add /usr/share/php/ to your PATH, give it the executable flag, then simply run
a2addvhost.php example.com
you may need to add the shebang at the beginning of the file.

Resources