bash 从 .bash_profile 获取目录中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1423352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
source all files in a directory from .bash_profile
提问by Mike Bannister
I need to allow several applications to append to a system variable ($PYTHONPATH in this case). I'm thinking of designating a directory where each app can add a module (e.g. .bash_profile_modulename). Tried something like this in ~/.bash_profile:
我需要允许多个应用程序附加到系统变量(在这种情况下为 $PYTHONPATH)。我正在考虑指定一个目录,每个应用程序都可以在其中添加一个模块(例如 .bash_profile_modulename)。在 ~/.bash_profile 中尝试过这样的事情:
find /home/mike/ -name ".bash_profile_*" | while read FILE; do
source "$FILE"
done;
but it doesn't appear to work.
但它似乎不起作用。
回答by Dirk Eddelbuettel
Wouldn't
不会
for f in ~/.bash_profile_*; do source $f; done
be sufficient?
够用吗?
Edit: Extra layer of ls ~/.bash_*
simplified to direct bash globbing.
编辑:额外的ls ~/.bash_*
简化层直接 bash globbing。
回答by Cascabel
I agree with Dennis above; your solution should work (although the semicolon after "done" shouldn't be necessary). However, you can also use a for loop
我同意上面的丹尼斯;您的解决方案应该有效(尽管“完成”后的分号不是必需的)。但是,您也可以使用 for 循环
for f in /path/to/dir*; do
. $f
done
The command substitution of ls is not necessary, as in Dirk's answer. This is the mechanism used, for example, in /etc/bash_completion
to source other bash completion scripts in /etc/bash_completion.d
ls 的命令替换不是必需的,如 Dirk 的回答。这是使用的机制,例如,在/etc/bash_completion
源中获取其他 bash 完成脚本/etc/bash_completion.d
回答by gaRex
Oneliner (only for bash/zsh):
Oneliner(仅适用于 bash/zsh):
source <(cat *)
回答by Faronitates
aus man bash:
aus man bash:
source filename [arguments]
源文件名 [参数]
ein source config/*
ein 源配置/*
The first argument will be sourced and all other files in config/ will be arguments to the script it sources.
第一个参数将被获取, config/ 中的所有其他文件将是它所获取的脚本的参数。
回答by sorin
for file in "$(find . -maxdepth 1 -name '*.sh' -print -quit)"; do source $file; done
This solution is the most postable I ever found, so far:
到目前为止,这个解决方案是我发现的最可发布的:
- It does not give any error if there are no files matching
- works with multiple shells including bash, zsh
- cross platform (Linux, MacOS, ...)
- 如果没有匹配的文件,它不会给出任何错误
- 适用于多种 shell,包括 bash、zsh
- 跨平台(Linux、MacOS 等)
回答by Christian Fr?hlich
str="$(find . -type f -name '*.sh' -print)"
arr=( $str )
for f in "${arr[@]}"; do
[[ -f $f ]] && . $f --source-only || echo "$f not found"
done
I tested this Script and I am using it. Just modifiy the . after find to point to your folder with your scripts and it will work.
我测试了这个脚本,我正在使用它。只需修改 . 在找到带有脚本的文件夹后,它将起作用。
回答by Eugene Yarmash
You can use this function to source all files (if any) in a directory:
您可以使用此函数获取目录中的所有文件(如果有):
source_files_in() {
local dir=""
if [[ -d "$dir" && -r "$dir" && -x "$dir" ]]; then
for file in "$dir"/*; do
[[ -f "$file" && -r "$file" ]] && . "$file"
done
fi
}
The extra file checks handle the corner case where the pattern does not match due to the directory being empty (which makes the loop variable expand to the pattern string itself).
额外的文件检查处理由于目录为空而导致模式不匹配的极端情况(这使得循环变量扩展为模式字符串本身)。
回答by mikejonesey
ok so what i ended up doing;
好吧,我最终做了什么;
eval "$(find perf-tests/ -type f -iname "*.test" | while read af; do echo "source $af"; done)"
this will execute a source in the current shell and maintian all variables...
这将在当前 shell 中执行一个源并维护所有变量......
回答by Yisrael Dov
I think you should just be able to do
我认为你应该能够做到
source ~/.bash_profile_*
source ~/.bash_profile_*