如何访问您在Bash中获取的文件的基本文件名? [英] How can you access the base filename of a file you are sourcing in Bash?
问题描述
我在一个需要导出一些环境变量的bash终端中采集一个文件。
I am sourcing a file in a bash terminal that needs to export some environment varibles.
示例:
source linux_x86.env
env文件看起来像这样:
the env file looks kinda like this:
export ARCH=/home/user/project/linux_x86
不同的架构进行编译,我想要能够做这样的事情:
I have a bunch of different architectures to compile for and I want be able to do something like this:
export ARCH=/home/user/project/`basename $0 .env`
其中 basename $ 0 .env
将给我的基本名称env文件
where basename $0 .env
would give me the basename the env file
bash linux_x86.env
linux_x86
上面的工作是一个bash脚本,但是似乎不工作,当你源文件。
The above will work is a bash script but doesn't seem to work when you source the file.
有没有办法从源头获得相同的行为?
Is there any way to get the same behavior from source?
推荐答案
p>请参阅 Bash脚本可以告诉它存储在哪个目录中?,特别是关于BASH_SOURCE变量的注释。
See Can a Bash script tell which directory it is stored in?, particularly the comment regarding the BASH_SOURCE variable.
摘要: SCRIPT_NAME = $(basename $ {BASH_SOURCE [0]})
这篇关于如何访问您在Bash中获取的文件的基本文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!