I have been using bash at work and zsh at home, different macbooks. I know very little bash or zsh scripting, so I didn't dare do the switch until I managed to get my ZSH on par with what I had at work, so that I wouldn't loose productivity.
I ended up making a .dot-files
repo on github, and wanted to test everything out. It worked. But when I decided to make one final adjustment, everything broke and for the life of me, I cannot understand why.
Scenario:
I have ~/.dot-files as the root of my git repo. Then two sub folders, zsh
and bash
. Inside of each I have hidden files, and a config that would like to source them.
$ cd ~/.dot-files/bash/
$ ls -la
drwxr-xr-x 8 me staff 256 May 8 13:28 .
drwxr-xr-x 7 me staff 224 May 8 13:00 ..
-rw-r--r-- 1 me staff 1791 May 8 13:08 .alias
-rw-r--r-- 1 me staff 79837 Apr 12 2022 .git-completion.bash
-rw-r--r-- 1 me staff 1602 May 8 13:49 .git-prompt
-rw-r--r-- 1 me staff 1816 May 8 13:06 .ps1
-rw-r--r-- 1 me staff 877 May 8 13:09 .utils
-rw-r--r-- 1 me staff 142 May 8 15:20 default-config
AND
$ cd ~/.dot-files/zsh/
$ ls -la
drwxr-xr-x 7 me staff 224 May 8 13:33 .
drwxr-xr-x 7 me staff 224 May 8 13:00 ..
-rw-r--r-- 1 me staff 1423 May 8 12:32 .alias
-rw-r--r-- 1 me staff 18582 May 8 12:32 .git-prompt.sh
-rw-r--r-- 1 me staff 2059 May 8 12:32 .prompt
-rw-r--r-- 1 me staff 926 May 8 14:01 .zsh-git-info
-rw-r--r-- 1 me staff 143 May 8 15:02 default-config
$ cat ~/.bashrc
source ~/.dot-files/bash/default-config
$ cat ~/.dot-files/bash/default-config
source "$(dirname -- "$0")/.git-prompt"
source "$(dirname -- "$0")/.ps1"
source "$(dirname -- "$0")/.alias"
source "$(dirname -- "$0")/.utils"
This does not work
-bash: ./.git-prompt: No such file or directory
-bash: ./.ps1: No such file or directory
-bash: ./.alias: No such file or directory
-bash: ./.utils: No such file or directory
$ cat ~/.zshrc
source ~/.dot-files/zsh/default-config
$ cat ~/.dot-files/zsh/default-config
source "$(dirname "$0")/.alias"
source "$(dirname "$0")/.prompt"
source "$(dirname "$0")/.zsh-git-info"
source "$(dirname "$0")/.git-prompt.sh"
This does work, perfectly.
What I'm trying to do
I want to change my shell chsh -s /bin/bash
, restart terminal. And I expect it to just work.
I have ~/.bashrc
I want to point it towards a repo that anyone can clone wherever they want.
I want that repo to reference relative path to where the default-config is. I want to NEVER have a hardcoded full path.
For zsh, source "$(dirname "$0")
just works. The steps as I understand them:
~/.zshrc
calls source on a file directly:~/.dot-files/zsh/default-config
- said file calls source command, yes, but it inserts "path to me" with
$(dirname "$0")
followed by the name of a file in the same folder as "me".source "$(dirname "$0")/.alias"
transforms tosource ~/.dot-files/zsh/.alias
Bash does not work, no idea why.
I know very little bash or zsh scripting
- many more people would be able to help you with bash issues than zsh issues since bash is a more commonly used shell (for evidence see how many questions have been asked at unix.stackexchange.com/questions/tagged/bash vs unix.stackexchange.com/questions/tagged/zsh - currently ~25,000 tagged bash vs ~3,000 tagged zsh) so if you don't know either and so may need help at times and bash is working as you want while zsh isn't, changing your zsh installation to bash instead of your bash one to zsh may be a better choice..alias
,.prompt
, etc files are located?source ./.alias
would look for the file in the working directory of the running script, only. That's likely your home directory, so it shouldn't find~/.dot-files/whatever/.alias
. Then again, if you did also have~/.alias
, I'm not sure why both Bash and zsh wouldn't find it. Unless you're testing on two different systems, where one has it, and the other doesn't.