LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 11-10-2016, 03:25 AM   #1
Steve Greig
LQ Newbie
 
Registered: Oct 2016
Posts: 27

Rep: Reputation: Disabled
Bash behaving unpredictably


I find when I open bash (I do this by clicking on the Konsole icon of a terminal at the bottom of the KDE screen and maybe sometimes by going into the KDE menu so I imagine I am always using the same version of Bash) I get variable behaviour.

For example sometimes the prompt is bash-4.3$ while other times it gives the root to the directory I am in and also tells me which user I am.

The output to commands like ls changes too. Sometimes I get it all in black (or at least blue) and white other times the files and directories are colour coded.

Today it seemed variable. I have pasted the bash session below which left me a bit confused:

bash-4.3$ which javac
/usr/lib64/java/bin/javac
bash-4.3$ su
Password:
bash-4.3# which javac
which: no javac in (/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin)
bash-4.3# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
bash-4.3# su steve
bash-4.3$ echo $PATH
/usr/local/bin:/bin:/usr/bin
bash-4.3$ which javac
which: no javac in (/usr/local/bin:/bin:/usr/bin)
bash-4.3$ whoami
steve
bash-4.3$

It seems odd that the first time as user steve I issue whereis javac it returns '/usr/lib64/java/bin/javac'. Then I change to user root and perhaps understandably it sais it cant find it in root's path. Then I su back to steve and reissue the command and now it sais it cant find it in steve's path.

I have just read the man page for which and it seems that which will only return the path to directories that are in the user's $PATH. Perhaps I misunderstood that because '/usr/lib64/java/bin/javac' was returned but it does not appear when I do echo $PATH.

Anything to push me in the right direction for understanding this would be very much appreciated, Steve
 
Old 11-10-2016, 03:30 AM   #2
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,096

Rep: Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173
to have a correctly initialized shell you have to start it as a login one: try with
Code:
su -
or
Code:
su - steve
this way /etc/profile and scripts in /etc/profile.d are executed (and PATH is set appropriately).

Last edited by ponce; 11-10-2016 at 03:31 AM.
 
4 members found this post helpful.
Old 11-10-2016, 05:53 AM   #3
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,096

Rep: Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173
if you want to read something short on the matter you can refer to this two links (one is actually from the man)

http://www.linuxfromscratch.org/blfs...s/profile.html
https://www.gnu.org/software/bash/ma...tup-Files.html
 
1 members found this post helpful.
Old 11-10-2016, 06:38 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
Setting up a ~/.bash_profile that runs ~/.bashrc as described in the second of the links that ponce provided you with is definitely the way to go. Some of the stuff in /etc/profile(.d/*) such as setting dircolors, PS1, and command aliases really belong in ~/.bashrc.
 
1 members found this post helpful.
Old 11-10-2016, 06:43 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,802

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
I have to say: not, it is not unpredictable, but it is the expected behaviour although it is a bit surprising.
You need to understand how the environment is set during login, by su and/or su - (and for example using ssh, opening a new terminal ....). Remember every and each shell has its own environment.
one more resource: man bash, look for invocation
 
1 members found this post helpful.
Old 11-10-2016, 06:45 AM   #6
bassmadrigal
LQ Guru
 
Registered: Nov 2003
Location: West Jordan, UT, USA
Distribution: Slackware
Posts: 8,792

Rep: Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656
Quote:
Originally Posted by Steve Greig View Post
It seems odd that the first time as user steve I issue whereis javac it returns '/usr/lib64/java/bin/javac'. Then I change to user root and perhaps understandably it sais it cant find it in root's path. Then I su back to steve and reissue the command and now it sais it cant find it in steve's path.
In addition to what ponce and GazL provided, if you were to run exit from your root session rather than su steve, it would've taken you back to your original login with the PATH variable set to include javac.
 
Old 11-11-2016, 08:35 AM   #7
MarcT
Member
 
Registered: Jan 2009
Location: UK
Distribution: Slackware 14.2
Posts: 125

Rep: Reputation: 51
As above, you must "su space dash (hyphen)", in order for su to run that user's login script:
Quote:
su -
To have the Konsole run a "login" shell by default (as I prefer), in Konsole click Settings > "Manage Profiles" > Select "Shell" & click "Edit Profile...", and change "Command:" to read:

Quote:
/bin/bash -l
(that's a lowercase [ L ], not an uppercase [ i ] - the font used by Firefox makes it hard to distinguish the difference) ...then click "OK".

The "-l" parameter tells bash to act as a "login" shell.

KRs,
Marc

Last edited by MarcT; 11-11-2016 at 08:38 AM.
 
2 members found this post helpful.
Old 11-14-2016, 04:10 AM   #8
Steve Greig
LQ Newbie
 
Registered: Oct 2016
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thanks very much for your help. It looks as if the initial non login shell has the path to javac but the login shell which I get to with su -l does not have the path. I guess I need to add that path to my $PATH variable.
 
Old 11-14-2016, 04:32 AM   #9
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,096

Rep: Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173Reputation: 4173
Quote:
Originally Posted by Steve Greig View Post
It looks as if the initial non login shell has the path to javac but the login shell which I get to with su -l does not have the path. I guess I need to add that path to my $PATH variable.
I think you are making a little confusion, it's exactly the opposite: the login shell that you get with "su -l" should have the correct PATH variable and the non-login one no.
you have described this exact situation in your first post.
 
1 members found this post helpful.
Old 11-14-2016, 04:43 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,802

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
that's why I suggested to read the man page (invocation) to understand what's happening. What is the login shell .... It is explained perfectly, I could only copy it....
 
1 members found this post helpful.
Old 11-14-2016, 01:25 PM   #11
Steve Greig
LQ Newbie
 
Registered: Oct 2016
Posts: 27

Original Poster
Rep: Reputation: Disabled
Woops! Thanks for putting me right. I guess the situation is that when I start up the computer I do log in right at the beginning and then go into KDE. Then I open a konsole window and am in a log in shell. If I issue su without a hyphen I am taken out of a login shell even though in one sense of the word login I am logging in (ie providing a username and password and being admitted to some functionality). I need to read up more on this. I did read the references given but a lot is not sinking in unfortunately. Some is!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash behaving oddly. lopid Slackware 4 11-15-2015 09:20 AM
[SOLVED] bash loops not behaving timl Linux - Software 5 07-20-2011 09:12 AM
Bash is behaving weird inside chroot ReyJavikVI Linux From Scratch 9 08-09-2009 08:48 PM
Bash behaving differently jxrod2000 Linux - Software 2 10-06-2008 05:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 02:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration