bash 在ubuntu中调用.bashrc文件中隐藏的shell脚本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22525735/
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
Call a hidden shell script file in .bashrc file in ubuntu
提问by user3359876
I've created a shell script and placed in home directory. but to make it invisible I put a DOT before it's name. I mean .filename.sh
. Now I want to call this file through .bashrc
file which is also invisible and placed in home directory.
我创建了一个 shell 脚本并放在主目录中。但为了让它不可见,我在它的名字前加了一个 DOT。我的意思是.filename.sh
。现在我想通过.bashrc
文件调用这个文件,该文件也是不可见的并放置在主目录中。
I tried
我试过
sh .filename.sh
and
sh filename.sh
both but no success.
sh .filename.sh
而
sh filename.sh
这两种,但没有成功。
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
. filename.sh #I'm trying to call this script here at the beginning of the file
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
...
... #some more content in .bashrc file
...
this shell script will print something on terminal whenever we open new tab or window in terminal.
每当我们在终端中打开新选项卡或窗口时,此 shell 脚本都会在终端上打印一些内容。
EDIT:
I tried . .filename.sh
but it works only when we are in home directory I mean (user@user$
)
but it should work for all directory, I mean no matter where am I (for instance suppose I'm in Desktop user@user:~/Desktop$
) and now if I open a new tab it shows error. It should work because when I put same content directly in .bashrc file instead of calling this sh file then it works well for all directory .
编辑:我试过了,. .filename.sh
但它只在我们在主目录中时才有效,我的意思是 ( user@user$
) 但它应该适用于所有目录,我的意思是无论我在哪里(例如假设我在 Desktop 中user@user:~/Desktop$
),现在如果我打开一个新的选项卡它显示错误。它应该可以工作,因为当我将相同的内容直接放在 .bashrc 文件中而不是调用这个 sh 文件时,它适用于所有目录。
回答by Saucier
If you want to source a file you can use two different notations. You could write
如果你想获取一个文件,你可以使用两种不同的符号。你可以写
source path/to/filename
but you could also write
但你也可以写
. path/to/filename
From the manual:
从手册:
". filename [arguments] ... This builtin is equivalent to source."
“. 文件名 [参数] ... 这个内置函数等同于源代码。”
Maybe in your case you are confused by the dot-notation. By prefixing your file with a dot you made it a hidden file. That means what you need is a second dot in front of your filename or just use the source
command which is in my opinion the better notation and harder to miss. :)
Also to be on the safe side you should include a path and use quotes in case you have characters in your filename that have to be escaped normally.
也许在您的情况下,您对点符号感到困惑。通过在您的文件前面加上一个点,您将其设为隐藏文件。这意味着您需要的是文件名前面的第二个点,或者只使用source
我认为更好的符号并且更难错过的命令。:) 另外,为了安全起见,您应该包含一个路径并使用引号,以防您的文件名中包含必须正常转义的字符。
As a result the following should do it considering your file exists in the root of your home directory. I added a test to check for its existence:
因此,考虑到您的文件存在于您的主目录的根目录中,以下应该这样做。我添加了一个测试来检查它的存在:
import="~/.filename.sh"
if [[ -f "$import" ]]; then
source "$import"
else
echo "Could not source ${import}. File does not exist."
fi
回答by Kartik Anand
Try using the following if .bashrc and the the script are in the same directory
如果 .bashrc 和脚本在同一目录中,请尝试使用以下内容
. ./.filename.sh
Or use this if the script is present in the home directory
或者如果脚本存在于主目录中,则使用它
. ~/.filename.sh