您如何访问您在Bash中采购的文件的基本文件名? [英] How can you access the base filename of a file you are sourcing in Bash?

查看:134
本文介绍了您如何访问您在Bash中采购的文件的基本文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个需要导出一些环境变量的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆