簡體   English   中英

當bash在CentOS中啟動時,如何自動設置別名?

[英]How to automatically set an alias when bash starts up in CentOS?

當bash終端啟動時,我希望它自動設置一些別名,但是由於某些原因它們不能正常工作。

我有一個名為.bash_aliases的bash腳本,在.bashrc文件中,我具有以下代碼:

if [ -f .bash_aliases ]; then
        echo "bash_aliases file is here" 
        ./.bash_aliases
fi

在文件.bash_aliases我有:

"creating alias for baaa"
alias baaa='echo "baaa"'

現在,當我啟動終端時,我看到:

bash_aliases file is here
creating alias for baaa

但是當我嘗試命令時:

baaa

我明白了嗎

bash: baaa: command not found...

這有什么意義? 但是,當我從終端執行操作時,

[<>@<> ~]$ alias baaa='echo "baaa"'
[<>@<> ~]$ baaa
baaa

這是預期的。 為什么在.bash_aliases文件中設置別名.bash_aliases 我確保該文件是可執行文件: sudo chmod 777 .bash_aliases

我正在使用CentOS 7。

標點很重要。 對於要添加的別名,您需要提供定義它們的腳本的源代碼 ,並在當前的shell中執行它們:

# WORKS: source file named bash_aliases in the current directory
. ./.bash_aliases       # POSIX-compliant syntax

也寫成

# WORKS: source file named bash_aliases in the current directory
source ./.bash_aliases   # bash-only syntax

如果相反,請執行以下操作:

# BROKEN: Run .bash_aliases as subprocess
./.bash_aliases

...它作為一個單獨的腳本運行,並且在該腳本退出時會忘記別名。


順便說一句-由於采購腳本會將其讀取到當前shell中,而不是將其作為單獨的程序執行,因此這意味着它不需要文件許可權即可執行 因此,您的.bash_aliases不需要也不應具有+x權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM