我一直在工作中使用bash,在家使用zsh,不同的macbooks。我对bash或zsh脚本知之甚少,所以我不敢做这个切换,直到我设法使我的ZSH与我在工作中所拥有的一样,这样我才不会降低生产率。
最后,我在github上做了一个.dot-files回购,并且想测试所有的东西。啊,真灵。但是当我决定做最后一次调整时,一切都崩溃了,而对于我的生活,我不明白为什么。
设想情况:
我有~/.点文件作为我的git的根。然后是两个子文件夹,zsh和bash。在每个文件的内部,我都有隐藏的文件,以及一个想要源源化它们的配置。
$ 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和
$ 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 ~/./bash/default-config
source "$(dirname -- "$0")/.git-prompt"
source "$(dirname -- "$0")/.ps1"
source "$(dirname -- "$0")/.alias"
source "$(dirname -- "$0")/.utils"这不管用
-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 ~/./zsh/default-config
source "$(dirname "$0")/.alias"
source "$(dirname "$0")/.prompt"
source "$(dirname "$0")/.zsh-git-info"
source "$(dirname "$0")/.git-prompt.sh"这确实很好,完美。
我想改变我的外壳chsh -s /bin/bash,重新启动终端。我希望它能起作用。
我有~/.bashrc,我想把它指向一个回购,任何人都可以克隆他们想要的任何地方。
我希望回购引用默认配置所在的相对路径。我不想有一条硬编码的全路。
对于zsh,source "$(dirname "$0")只是起作用。据我所知,这些步骤:
~/.zshrc直接调用文件上的源:~/.dot-files/zsh/default-config$(dirname "$0")插入“到我的路径”,然后在与"me“相同的文件夹中插入文件的名称。source "$(dirname "$0")/.alias"变换为source ~/.dot-files/zsh/.alias巴什不起作用,不知道为什么。
发布于 2023-05-08 13:56:10
有两种解决问题的简单方法:
pushd/popd只需更改default-config文件中的当前文件夹,然后恢复:
if pushd ~/.dot-files/bash/ > /dev/null; then
source ./.alias
source ./.prompt
source ./.zsh-git-info
source ./.git-prompt.sh
popd > /dev/null
fi(在zsh中,您可以使用-q而不是将stdout重定向到/dev/null,以避免目录堆栈的转储,但在bash中则不然)。
当然,您的default-config文件在这一行中会有所不同。
在.bashrc和.zshrc中定义一个变量。该变量将指向源文件所在的位置:
# .bashrc
fdir=~/.dot-files/bash
source "${fdir}/default-config"对.zshrc也这样做
在default-config内部,对于两个shell可以是相同的,只需重用这个变量:
source "${fdir}/.alias"
source "${fdir}/.prompt"
source "${fdir}/.zsh-git-info"
source "${fdir}/.git-prompt.sh"https://unix.stackexchange.com/questions/745267
复制相似问题