Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何包含相对脚本源文件bashrc_Linux_Bash - Fatal编程技术网

Linux 如何包含相对脚本源文件bashrc

Linux 如何包含相对脚本源文件bashrc,linux,bash,Linux,Bash,我有多个bash文件。 我想写一个主bash文件,其中包括当前目录中所有必需的bash文件。 我试过这样做 #!/bin/bash HELPER_DIR=`dirname $0` .$HELPER_DIR/alias 但当我把下面这句话放在我的 $HOME/.bashrc if [ -f /home/vivek/Helpers/bash/main.bash ]; then . /home/vivek/Helpers/bash/main.bash fi 我没有收到这样的文件。/alia

我有多个bash文件。 我想写一个主bash文件,其中包括当前目录中所有必需的bash文件。 我试过这样做

#!/bin/bash
HELPER_DIR=`dirname $0`
.$HELPER_DIR/alias
但当我把下面这句话放在我的 $HOME/.bashrc

if [ -f /home/vivek/Helpers/bash/main.bash ]; then
    . /home/vivek/Helpers/bash/main.bash
fi
我没有收到这样的文件。/alias。文件别名在那里。如何包含相对bash文件?

使用
$(dirname“${bash\u SOURCE[0]}”)

我将这两行添加到两个my
~/.bashrc

echo '$0=' $0
echo '$BASH_SOURCE[0]=' ${BASH_SOURCE[0]}
并启动了bash:

$ bash
$0= bash
$BASH_SOURCE[0]= /home/igor/.bashrc
当您使用
源代码
(或
)或
~/.启动脚本时,$0和$BASH\u SOURCE之间存在差异。bashrc
使用
$(dirname“${BASH\u SOURCE[0]}”)

我将这两行添加到两个my
~/.bashrc

echo '$0=' $0
echo '$BASH_SOURCE[0]=' ${BASH_SOURCE[0]}
并启动了bash:

$ bash
$0= bash
$BASH_SOURCE[0]= /home/igor/.bashrc

当您使用
源代码
(或
)或
~/)启动脚本时,$0和$BASH\u源代码之间存在差异。bashrc

您需要在“点”后面留一个空格


你需要在“点”后面留下一个空格

$(dirname“${BASH_SOURCE[0]}”)
如果从同一目录调用脚本,则返回
,如果使用相对路径(如
。/myscript.sh
)调用脚本,则返回相对路径

我使用
script\u dir=$(cd“$(dirname“${BASH\u SOURCE[0]}”)”&&pwd)
获取脚本所在的目录

下面是测试此功能的示例脚本:

#!/bin/bash
# This script is located at /home/lrobert/test.sh

# This just tests the current PWD
echo "PWD: $(pwd)"


# Using just bash source returns the relative path to the script
# If called from /home with the command 'lrobert/test.sh' this returns 'lrobert'
bash_source="$(dirname "${BASH_SOURCE[0]}")"
echo "bash_source: ${bash_source}"


# This returns the actual path to the script
# Returns /home/lrobert when called from any directory
script_dir=$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
echo "script_dir: ${script_dir}"

# This just tests to see if our PWD was modified
echo "PWD: $(pwd)"
$(dirname“${BASH_SOURCE[0]}”)
如果从同一目录调用脚本,则返回
,如果使用相对路径(如
。/myscript.sh
)调用脚本,则返回相对路径

我使用
script\u dir=$(cd“$(dirname“${BASH\u SOURCE[0]}”)”&&pwd)
获取脚本所在的目录

下面是测试此功能的示例脚本:

#!/bin/bash
# This script is located at /home/lrobert/test.sh

# This just tests the current PWD
echo "PWD: $(pwd)"


# Using just bash source returns the relative path to the script
# If called from /home with the command 'lrobert/test.sh' this returns 'lrobert'
bash_source="$(dirname "${BASH_SOURCE[0]}")"
echo "bash_source: ${bash_source}"


# This returns the actual path to the script
# Returns /home/lrobert when called from any directory
script_dir=$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
echo "script_dir: ${script_dir}"

# This just tests to see if our PWD was modified
echo "PWD: $(pwd)"