Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

linux - bashrc not loading until run bash command

I am running freshly installed Arch Linux. When I log into a user (running bash) and try to use an alias from .bashrc, it gives me the 'command not found' error. But, if I reenter bash via the 'bash' command, the command works just fine.

Yes, I am already in bash.

env initially:

SHELL=/usr/bin/bash

env after running bash, it remains:

SHELL=/usr/bin/bash

So I'm not quite sure where the problem is.

question from:https://stackoverflow.com/questions/18393521/bashrc-not-loading-until-run-bash-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read the INVOCATION section from "bash(1)" for full details (that's the man page for bash; use man bash). Your first shell upon logging in is a "login shell", which means that the .bashrc file is not sourced. Your second invocation creates an interactive shell, where .bashrc is sourced.

If you always want the content of your .bashrc file processed, you can add the following lines to your .bash_profile file, creating that file if it does not already exist:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

Per its man page, bash "[...] looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable." Conventions and policies of your local system will determine which, if any, of these files already exist.

A word of caution: be aware that creating a new .bash_profile in your home directory could have the unintended side-effect of preventing the reading and executing of commands in a .bash_login or .profile file already present, changing further the behavior of subsequent logins.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...