4

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.

T Anna
  • 874
  • 5
  • 21
  • 52
  • After you add anything to `.bashrc`, you must **source** your `.bashrc` for the changes to be seen. So make your changes and then at the command prompt enter `. ~/.bashrc` (that's "*dot space* `~/.bashrc`") – David C. Rankin Oct 13 '18 at 20:10
  • When i do this, the command doesn't end, and I had to press ctrl+C to exit. Then i tried to open a new terminal and I got a blank terminal again. – T Anna Oct 13 '18 at 20:21
  • That means you messed something up in your `.bashrc` causing it not to source cleanly. Log in as `root` (or another user and `su`) then change to your user directory and edit your `~/.bashrc` and remove the offending issue. – David C. Rankin Oct 13 '18 at 20:24

1 Answers1

9

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

yosefrow
  • 2,128
  • 20
  • 29
  • Thanks, your solution worked but I don't understand it. When you say login subshell, do you know mean that I am logged in to my system as a specific user and for specific users, only .bashrc is read and not .profile? But, then, you say .profile calls .bashrc. Sorry, I am confused. Could you please explain this in a simple way. – T Anna Oct 13 '18 at 20:25
  • The link that you shared says, .profile is for things that are not specifically related to Bash, like environment variables PATH and friends, and should be available anytime. Then why do we add the path of kafka to .bashrc? – T Anna Oct 13 '18 at 20:28
  • I rewrote it, please tell me if it is clear now. There are different kinds of Shells. Bash is just the default in most modern systems. In the Ubuntu systems I know .profile is called only in login shells. Therefore it is not called by default in the desktop terminal – yosefrow Oct 13 '18 at 20:32
  • regardless of whether a shell is a login type or not, the shell itself can be bash, or dash, or other kinds of shells – yosefrow Oct 13 '18 at 20:35
  • @TAnna by the way, if my answer was useful, please mark it as accepted, so the next person who sees this question knows which answer worked. Thanks! – yosefrow Oct 13 '18 at 20:35
  • "*only ~/.bashrc -- not ~/.profile is sourced in subshells*" A sub-shell does *not* source anything, a sub-shell is a command-list inside parentheses, you are probably thinking of a non-login interactive shell. – cdarke Oct 13 '18 at 21:33
  • 3
    If you have a `.bash_profile` (even if it is empty) then `.profile` will not be run. From `man bash`: *After reading that file, it 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.* – cdarke Oct 13 '18 at 21:34
  • @cdarke Hi, thanks alot for your corrections. I have added them to the answer. Please let me know if you see anything else that needs correcting. – yosefrow Oct 13 '18 at 23:18
  • @cdarke, I've looking around the web, and your comment is what I was looking for. Thank you very much. – sk001 Nov 11 '21 at 18:28