Table of Contents
Linux user source .bashrc or .profile file cannot be found
The difference between Linux .bashrc .bash_profile and .profile
1 Overview
2 Interactive and non-interactive shells
3 bash startup file
4 Difference
Home Operation and Maintenance Linux Operation and Maintenance How to solve the problem that Linux user source .bashrc or .profile cannot find the file

How to solve the problem that Linux user source .bashrc or .profile cannot find the file

May 13, 2023 pm 09:37 PM
linux

    Linux user source .bashrc or .profile file cannot be found

    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 difference between Linux .bashrc .bash_profile and .profile

    1 Overview

    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.

    2 Interactive and non-interactive shells

    • 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.

    3 bash startup file

    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.."
    Copy after login

    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 ~]$
    Copy after login

    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.."
    Copy after login

    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 ~]$
    Copy after login

    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.

    4 Difference

    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
    Copy after login

    注意:

    总而言之,在使用环境之前,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!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

    VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

    Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

    The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

    How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

    Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

    vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

    vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

    vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

    The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

    How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

    To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

    Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

    Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

    Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

    VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

    See all articles