您如何访问您在Bash中采购的文件的基本文件名? [英] How can you access the base filename of a file you are sourcing in Bash?
问题描述
示例:
source linux_x86.env
env文件看起来像这样:
export ARCH = / home / user / project / linux_x86
我有一堆不同的架构来编译,我想要这样做:
export ARCH = / home / user / project /`basename $ 0 .env`
其中 basename $ 0 .env
将给我的基本名称env文件
bash linux_x86.env
linux_x86
上面的工作是一个bash脚本,但在您提供文件时似乎不起作用。
有没有办法从源头获得相同的行为?
p>请参阅从,特别是关于BASH_SOURCE变量的评论。
总结: SCRIPT_NAME = $(basename $ {BASH_SOURCE [0]}) code>
I am sourcing a file in a bash terminal that needs to export some environment varibles.
Example:
source linux_x86.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`
where basename $0 .env
would give me the basename the env file
bash linux_x86.env
linux_x86
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?
See Getting the source directory of a Bash script from within, particularly the comment regarding the BASH_SOURCE variable.
Summary: SCRIPT_NAME=$(basename ${BASH_SOURCE[0]})
这篇关于您如何访问您在Bash中采购的文件的基本文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!