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)

    How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

    Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

    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)

    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.

    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.

    What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

    VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

    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.

    What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

    The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

    How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

    Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

    See all articles