Home > Article > Operation and Maintenance > How to solve the problem that Linux user source .bashrc or .profile cannot find the file
I encountered this situation in debian before. Newly added users log in every time The paths all show that sh-42$ requires su - username to return to normal, and there is no way to source .bashrc and other configuration files in the user directory, causing a series of problems.
If this is the case, it is very likely that the default startup shell of Linux is incorrect. Switch su to the super user, and use vi /etc/passwd to view the corresponding startup shell of the user and compare it with the startup shells of other normal users. , if they are different, just modify them to be the same. .
For example, if the shell of other normal users is /bin/bash and the shell of abnormal users is /bin/sh, change it to /bin/bash.
The bash shell uses some startup files to set environment variables. These startup files are the shell itself and System users determine certain bash shell configurations, in this article we will understand the difference between .bashrc .bash-profile and .profile.
Bash provides two mode options in the interactive shell, login and non-login (login and non-login) .
When we use ssh to log in to the system, we get an interactive login shell (interactive login shell), which reads the startup file when called.
However, when we call a new shell on an already logged-in shell, we get an interactive, non-login shell. This shell only executes the .bashrc file
When the shell does not require any human intervention to execute commands, we call it a non-interactive shell. For example, when a script spawns a subshell to execute a command, the subshell is a non-interactive shell, the subshell does not execute any startup files, it inherits environment variables from the shell that created it.
The startup file contains commands that need to be executed when the shell starts. Therefore, the shell automatically executes the commands in these startup files to set up the shell. This process occurs before the command prompt is displayed.
3.1 The significance of .bash_profile
The .bash_profile file contains commands for setting environment variables, so the shell will inherit these variables.
In an interactive login shell, bash first looks for the /etc/profile file. If found, bash will read and execute it in the current shell. The result is that /etc/profile sets the environment configuration for all users
Similarly, bash then checks whether .bash_profile exists in the home directory (the directory entered by cd ~ as the home directory). If present, bash executes .bash_profile in the current shell, and Bash then stops looking for other files such as .bash_login and .profile.
If bash does not find .bash_profile, then it will look for .bash_login and .profile in order and only execute the first readable file.
Let's study a sample .bash_profile file. Here we reset and export the PATH variable
echo "Bash_profile execution starts.." PATH=$PATH:$HOME/bin; export PATH; echo "Bash_profile execution stops.."
Before logging into the command prompt of the interactive shell, we will see the following output
Bash_profile execution starts.. Bash_profile execution stops.. [example@example ~]$
3.2 The meaning of .bashrc
.bashrc contains commands specific to the bash shell. Every interactive non-login shell reads .bashrc first, and generally, .bashrc is the best place to add aliases and bash-related functionality.
The bash shell looks for the .bashrc file in the home directory and uses source to execute it in the current shell.
Let's get to know the .bashrc file through a sample
echo "Bashrc execution starts.." alias elui='top -c -u $USER' alias ll='ls -lrt' echo "Bashrc execution stops.."
Before the command prompt of the interactive non-login shell, we will see the following output
[example@example ~]$ bash Bashrc execution starts.. Bashrc execution stops.. [example@example ~]$
3.2 The meaning of .profile
During the interactive shell login process, if .bash_profile does not exist in the home directory, bash looks for .bash_login. If **.bash_login** is found, bash executes it. If .bash_login does not exist in the home directory, bash looks for .profile and executes it.
.profile can maintain the same configuration as .bash_profile or .bash_login. It controls which prompts appear, keyboard sounds, which shell to open, and individual profile settings that override variables set in the /etc/profile file.
The bash shell will execute .bash_profile every time you log in interactively. If .bash_profile is not found in the home directory, bash will execute the first readable file found from .bash_login and .profile. However, on every interactive non-login shell start, bash creates .bashrc.
Normally, environment variables will be put into .bash_profile. Since the interactive login shell is the first shell, all the default settings required for environment setup are put into **.bash_profile**. Therefore, they are set only once and are inherited in all subshells.
Likewise, aliases and functions will also be put into .bashrc. Make sure these are loaded every time you start a shell from an existing environment.
However, to avoid login and non-login interactive shell setup difference. .bash_profile calls .bashrc. Therefore, we will see the following code snippet inserted into **.bash_profile** so that on every interactive login shell .bashrc is also executed in the same shell:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi PATH=$PATH:$HOME/bin export PATH
注意:
总而言之,在使用环境之前,shell需要其启动文件以配置shell环境。
在本文中,我们检查了各种shell模式。然后,我们了解了各种bash启动文件的重要性。最后,我们检查了这些启动文件之间的差异。
The above is the detailed content of How to solve the problem that Linux user source .bashrc or .profile cannot find the file. For more information, please follow other related articles on the PHP Chinese website!