Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$PATH contains spaces instead of colons #1744

Closed
franciscolourenco opened this issue Oct 7, 2014 · 13 comments
Closed

$PATH contains spaces instead of colons #1744

franciscolourenco opened this issue Oct 7, 2014 · 13 comments

Comments

@franciscolourenco
Copy link

bash -> /usr/local/bin:/usr/local/sbin:/usr/bin
fish -> /usr/local/bin /usr/local/sbin /usr/bin

Isn't this bad for programs which might need to parse $PATH and are expecting directories to be separated by colons?

@ridiculousfish
Copy link
Member

$PATH does not contain spaces. It is a list. When it is exported, it is colon delimited.

@franciscolourenco
Copy link
Author

You mean, when it is exported in bash?
Because echo $PATH in fish results in spaces..

@ridiculousfish
Copy link
Member

When fish exports PATH, it is colon-delimited. Try env | grep PATH to see.

echo $PATH looks at the value of the variable in fish (where it's a list), not the value of the variable in the environment.

@franciscolourenco
Copy link
Author

Thats what I ended up using in here franciscolourenco/SublimeFixMacPath@b9369ed

I think I understand what you are saying..
Wouldn't it be better if fish used colons to represent different elements of a list?

@ridiculousfish
Copy link
Member

So lists in fish are like arrays in other languages (e.g. Python or JavaScript). They're not delimited by anything; they're an indexed sequence of values under a single variable name.

When you pass a list to a command, each value in the list becomes a separate argument. echo makes it appear space delimited, because echo just space-separates its argument. But you can do other stuff with lists too:

> set files first.jpg second.jpg third.jpg
> cp $files images/

Certainly in that case we want each file to be a separate argument, not a single colon-separated argument.

PATH is interpreted as a list, which brings lots of advantages. For example, to delete the last element in PATH, it's just set -e PATH[-1]. To loop over the directories in PATH, it's just for dir in $PATH. To do either in bash would require sed or an equivalent. This is why we like our lists!

I took a look at the commit. It's gratifying to see improved fish support in Sublime 👍 Using env instead of echo is a good fix, since the output will be independent of shell and it will insulate you from these sorts of differences.

Hope that clears things up!

@franciscolourenco
Copy link
Author

Great explanation, thank you a lot for answering so promptly!

@ridiculousfish
Copy link
Member

You're welcome, and thanks for taking the time to write this up.

@KamilaBorowska
Copy link
Contributor

To do either in bash would require sed or an equivalent.

Actually, no. But it's not easy either, as you need to modify a special variable. () starts a subshell, to avoid scoping IFS for too long. Also, it doesn't work in other shells (such as zsh).

(
    IFS=:
    for path in $PATH
    do
        echo "Found path: $path."
    done
)

@coolaj86
Copy link

coolaj86 commented Jul 20, 2019

Is it possible for fish to read bash PATH exports?

newpath.sh:

export PATH="/new/path:$PATH"
source newpath.sh

Is there an adjustment I could make so that this will work in both fish and bash?

@faho
Copy link
Member

faho commented Jul 20, 2019

@solderjs: In fish >= 3.0, that'll work, as fish splits $PATH on colons since then, and joins it with colons in quotes.

HOWEVER: In general, it can't be guaranteed that a script will work in bash and fish, and the common subset is quite small (no blocks at all - no if or for).

Also questions like this are better asked at gitter.im/fish-shell/fish-shell instead of in closed issues.

@coolaj86
Copy link

Ah, I'm on fish 2.5.0

Also questions like this are better asked at gitter.im/fish-shell/fish-shell instead of in closed issues.

However, Google can't find things on gitter (or if it can, it doesn't rank them), and since my search lead me here, it will likely lead someone else (such as future me) here again.

@faho
Copy link
Member

faho commented Jul 20, 2019

Ah, I'm on fish 2.5.0

See https://fishshell.com for our variety of PPAs and repositories, or build it yourself.

@coolaj86
Copy link

coolaj86 commented Jul 28, 2019

Thanks. I got it working.

This is what I was creating:

Pathman
The cross-platform, cross-shell PATH manager

https://git.rootprojects.org/root/pathman

Works on Windows, Mac, Linux, Bash, Fish, ZSH, cmd.exe, and PowerShell

It can

  • add
  • remove
  • list
pathman help

*nix

pathman add ~/.local/bin
Saved PATH changes. To set the PATH immediately, update the current session:

	export PATH="/Users/me/.local/bin:$PATH"

Windows

pathman add ~\.local\bin
Saved PATH changes. To set the PATH immediately, update the current session:

	PATH \Users\me\.local\bin;%PATH%

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants