1

I want to set my path (and some other environment variables) based on a certain directory, so I have the following file in my home directory

export MY_DIR="/path/to/dir"
export PATH=$MY_DIR/bin:$PATH
export MY_OTHER_VAR="$MY_DIR/folder"

I source that in my ~/.zshrc with the following line: [[ -e ~/my_file.sh ]] && emulate sh -c ' source ~/my_file.sh

However, when I log in, $MY_DIR/bin is not in my path. If I echo $PATH, I get %F{51}~%f where $MY_DIR/bin should be. However, both MY_DIR and MY_OTHER_VAR were set correctly.

Have I done something wrong, or is this some weird bug in zsh (it seems to work as expected with bash)?

Edit

I have used both source and ~ elsewhere in my zshrc without issue. However, I tried moving the file to /etc/zsh/my_file.sh and sourcing it from etc/zsh/zprofile. I also tried using . rather than source, but even with those changes, I still see the same %F{51}~%f in my path variable. Actually, I've noticed that I see whatever my current directory is in the PATH variable, so for example, if I navigate to /etc/zsh/ and echo $PATH, I see %F{51}/etc/zsh%f.

Full code to reproduce:

/etc/zsh/zprofile:

[[ -e /etc/zsh/my_file.sh ]] && emulate sh -c ' . /etc/zsh/my_file.sh'

/etc/zsh/my_file.sh:

export MY_DIR="/home/matt/code"
export PATH="$MY_DIR/bin:$PATH"
export MY_PYTHON="$MY_DIR/python"

EDIT 2

So I am using a custom prompt which prints my current path in cyan. If I disable that prompt, my PATH variable gets set properly. In the script for the prompt, I use a variable that I call path (lowercase). Apparently, while bash doesn't confuse it with PATH, zsh does. Thanks to all for the help.

12
  • 1
    Do you have the same effect with this same exact code example? I am asking because there is a syntax error in the example (the lone " on the end of the last line) and it looks like you changed the variable names and paths in order to "anonymize" the question. This could hide the actual issue, as it might have to do with the chosen variable names or the exact way how they are used. %F{51}~%f looks like it might be part of the prompt or some output (print -P '%F{51}~%f' will print a cyan colored ~).
    – Adaephon
    Commented Jun 14, 2018 at 7:51
  • 1
    sh generally does not support source (but I'm not sure about zsh emulation of sh). In sh, you would use . (dot) instead. Also, I'm likewise unsure how zsh treats the quoted tilde in this case. I would use $HOME instead.
    – Kusalananda
    Commented Jun 14, 2018 at 9:09
  • 2
    There's undoubtedly an error in your code, but it doesn't seem to be in the part that you show, other than the missing quotes which wouldn't have this behavior. The missing quotes clearly show that you aren't showing us your actual code. Without seeing your code, we can't help you. Copy-paste complete code that reproduces the problem into your question. Commented Jun 14, 2018 at 9:32
  • sorry for the typo, I typed this question on mobile while on the go, which was probably a bad idea... Aside from the missing quotes, all I changed are the names of the paths and variables. Anyway, I've addressed all the comments in an edit Commented Jun 14, 2018 at 16:17
  • As @Adaephon suggested, the issue was related to my prompt (see my second edit). Thanks for the help. Commented Jun 14, 2018 at 17:16

1 Answer 1

1

My issue was actually not in the code where I was attempting to set the path to "$MY_DIR/bin:$PATH", but in a different script that I was sourcing in my .zshrc. I'm using a custom script to set my shell prompt in which I used a variable declared as path, thinking that would not conflict with the environment variable PATH, but, as pointed out by @Adaephon in the comments, zsh bidirectionally synchronizes the path keyword with PATH.

Thus, for me, the solution was to rename the variable in my prompt function to cur_path (or similar). Attempting to define the variable as local path will result in a segfault.

1
  • 1
    You can define a local variable $path that does not behave like the special parameter $path, if you "hide" its specialness: local -h path. Alternative, you can define a local variable $path that does behave like the special parameter $fpath if you invert the "hide" flag: local +h path. Commented Apr 27, 2021 at 7:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .