Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble with Ubuntu .profile and .bashrc

I am new to linux. I am currently going through a setup tutorial for Kafka online. It says to add the path of my kafka bin directory as follows to my .profile file which I did as below:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

# set PATH so it includes user's private bin directories
DOCKER="/usr/local/bin/docker-compose"
PATH="$HOME/bin:$HOME/.local/bin:$PATH:$DOCKER"
# Ubuntu make installation of Ubuntu Make binary symlink
PATH=/home/username/.local/share/umake/bin:$PATH
cat ~/.ssh/config.d/* > ~/.ssh/config
PATH="$PATH:/home/username/softwares/kafka/kafka_2.11-1.0.0/bin"

After that, i did an echo on $PATH, I could see the kafka path added to the PATH. Post which, i typed kafka and then tab, then i could see kafka related optional commands.

The tutorial then tells to edit .bashrc file. It was already there. It was quite a big file. I added at the end of the file the below line(as per the tutorial):

. ~/.profile

After that, i opened another terminal as per the tutorial to see that I am still getting all the kafka related options after kafka and tab. I saw that on the terminal, i was not getting my username and it was a blank terminal window. I then edited the .bashrc to remove the line I added and then tried to open a new terminal, and I could see my username on the terminal. Then i closed all the terminals. Opened a fresh one and typed kafka and tab, and I didnt get any options as earlier. I then opened the .profile file and i could see the kafka path still added. Then, I tried to echo $PATH, and this time, the kafka path was not there.

I am really confused what is happening here. Could you please explain a bit and let me know how to load the .profile every time i open a terminal and that why do I not see the kafka path anymore , when i do an echo on PATH.

like image 283
T Anna Avatar asked Jan 28 '23 07:01

T Anna


1 Answers

What is happening:

Sourcing:

When you run the command . somefile you are sourcing that file into your current shell which basically means it runs every command that is in somefile in your current shell.

Some files are automatically sourced under certain conditions.

Shell Types:

Interactive Shell: A shell which is intended for typing commands and receiving output. In bash, you can create an interactive shell in these ways:

  1. Login with a bash shell
  2. Run bash from a terminal

Login Shell: shell that is created when you first login. ie. shell you receive when you first ssh into a server or login into a machine with no GUI.

Non-Login Interactive Shell: Shell that is not a Login shell but is interactive. ie. Shell that is created when you open a desktop terminal application or shell that you get when you run bash after being logged in via ssh

~/.profile and ~/.bash_profile, and ~/.bash_login can be automatically sourced by login shells So, probably you are getting this path when you login for the first time or manually source (.) .profile

Please note that If .bash_profile exists and is readable, then Bash will not read .bash_login or .profile. https://askubuntu.com/questions/98433/run-a-script-on-login-using-bash-login

The login shell 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" man bash (credit: cdarke)

You are probably trying to open bash inside a desktop terminal or secondary bash shell after that and you are not getting the new path because only ~/.bashrc -- not ~/.profile is sourced in Non-Login Interactive Shells, and you do not have this PATH directive in your ~/.bashrc

Solution:

Add PATH="$PATH:/home/username/softwares/kafka/kafka_2.11-1.0.0/bin" to your ~/.bashrc instead of to ~/.profile

Reference:

https://serverfault.com/questions/261802/what-are-the-functional-differences-between-profile-bash-profile-and-bashrc

Note:

Don't worry about it not showing up in login shells, because as you see .profile calls .bashrc

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

Thanks to cdarke for corrections about terminology and Note about how login configurations work

like image 163
yosefrow Avatar answered Jan 29 '23 19:01

yosefrow