LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-20-2008, 09:36 PM   #1
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Rep: Reputation: 15
how to change current bash to a login shell


if run "bash -l", ps will show two bash

PID TTY TIME CMD
10546 pts/5 00:00:00 bash
10578 pts/5 00:00:00 bash

is there a way to change "current" bash to be a login shell?
 
Old 10-20-2008, 10:00 PM   #2
smallville
Member
 
Registered: Dec 2005
Posts: 44

Rep: Reputation: 15
http://www.labtestproject.com/linuxcmd/chsh.html
 
Old 10-20-2008, 10:39 PM   #3
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
thanks for your replay, but chsh is not what i need.

maybe my words is not clear, what i want is:
use bash as a login-shell from non-login shell without adding new process.
 
Old 10-21-2008, 01:43 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
The program that launches the shell is responsible for setting argv[0], thus indicating to the shell that it will be a login shell. You don't change this after the fact.

What problem are you trying to solve?
 
Old 10-21-2008, 04:43 AM   #5
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
I want to save all my input on cmdline by adding "history -a" to ~/.bash_logout

So, i need a login-shell
 
Old 10-21-2008, 08:38 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
It is not required that a shell be a login shell in order to save the history:
Code:
$ bash
$ echo $$
4799
$ ps -p 4799  
 PID TTY   STAT    TIME COMMAND
4799 ttyp3 S    0:00.02 bash
$ ls
/tmp
$ who
mrc      ttyp0    Oct 17 12:34  (example.com)
mrc      ttyp1    Oct 17 12:34  (example.com)
mrc      ttyp2    Oct 20 21:37  (example.com)
mrc      ttyp3    Oct 21 06:33  (example.com)
$ exit
exit
$ tail -3 ~/.bash_history
ls
who
exit
Set HISTFILE in your .bashrc (or whatever startup file you use). See HISTFILE in man bash, and also all other HIST* variables.
 
Old 10-21-2008, 09:23 PM   #7
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
you are right if there is only one shell history in concern.
if I wanna store all my input on any running shell cmdline, non-login shell is not qualified.
for example:
Code:
$ ps
  PID TTY          TIME CMD
11192 pts/4    00:00:00 bash
11653 pts/4    00:00:00 ps
$ echo "before history test"
before history test
$ history -a
$ echo "begin history test"
begin history test
$ bash
$ echo "in history test"
in history test
$ exit
exit
$ echo "end history test"
end history test
$ history |tail -9
 1236  ps
 1237  history
 1238  ps
 1239  echo "before history test"
 1240  history -a
 1241  echo "begin history test"
 1242  bash
 1243  echo "end history test"
 1244  history |tail -9
all my input after --->echo "in history test"<--- is lost in history.

Last edited by jackandking; 10-21-2008 at 09:26 PM.
 
Old 10-21-2008, 09:57 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
The current shell does not re-read the history file when a child shell exits. Each shell retains its own in-core (memory) history. When the shell exits, that history list gets written out. It is a last-wins policy when the history is not appended, but overwritten. See man bash:

Code:
HISTORY

       ....When an inter-
       active shell exits, the last $HISTSIZE lines are copied from  the  his-
       tory list to $HISTFILE.  If the histappend shell option is enabled (see
       the description of shopt under SHELL BUILTIN COMMANDS below), the lines
       are  appended  to the history file, otherwise the history file is over-
       written.  If HISTFILE is unset, or if the history file  is  unwritable,
       the  history  is not saved.  After saving the history, the history file
       is truncated to contain no more than HISTFILESIZE lines.  If  HISTFILE-
       SIZE is not set, no truncation is performed.
 
Old 10-22-2008, 12:06 AM   #9
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,038

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
In short, your issues has nothing to do with the shell being a login shell or not.

The only differences between a login shell and a non-login one are only relevant while the shell is starting (different rc files for login vs. non-login). There are some minor differences but nothing related to how the shell will work once it's been initialized. Maybe someone more knowledgeable can explain better

If all you want to do is to reinitialize the shell, you can fork a new one causing the parent one to close by doing

Code:
exec bash
Do it before your history|tail stuff and you will se that the output is quite different
 
Old 10-22-2008, 03:15 AM   #10
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
thanks to all of you!

"exec bash -l" can solve my problem.

"shopt -s histappend" should solve it too, but not working in my test(still last-win):

firstly, add "shopt -s histappend" to ~/.bashrc, then
Code:
$ ps
  PID TTY          TIME CMD
 3506 pts/12   00:00:00 bash
 3674 pts/12   00:00:00 ps
$ echo "before history test"
before history test
$ shopt |grep append
histappend      on
$ bash
$ echo "in history test"
in history test
$ exit
exit
$ echo "end history test"
end history test
$ history |tail -10
 1342  env
 1343  export PS1=$
 1344  export PS1=$\
 1345  ls
 1346  ps
 1347  echo "before history test"
 1348  shopt |grep append
 1349  bash
 1350  echo "end history test"
 1351  history |tail -10
still lost -->echo "in history test"<--
 
Old 10-22-2008, 03:24 AM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
I'll repeat - the CURRENT shell does not re-read the history FILE each command. It WRITES the file upon exit. It only READS the history file upon STARTUP. So, the history command you run is only showing you the current shell's IN CORE history list. You're asking a child shell to fill its parent's history list.
 
Old 10-22-2008, 04:59 AM   #12
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
finally, I understand,
thanks for your patient.
 
Old 10-22-2008, 08:40 AM   #13
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,038

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
That was the purpose of my "exec bash". It closes the current shell and substitutes it with a new one. That's why it works.
 
Old 10-22-2008, 11:24 AM   #14
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Right, and to make it REALLY appear / act as a login shell, add -a:

Code:
$ ps -p $$
  PID TTY   STAT    TIME COMMAND
10807 ttyp0 S    0:00.08 bash 

$ exec -a -bash bash

$ ps -p $$
  PID TTY   STAT    TIME COMMAND
10807 ttyp0 S    0:00.10 -bash
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to change working directory of the current shell? kevin_chn Programming 6 05-16-2008 02:26 AM
command to temporary change current shell. hocheetiong Linux - Newbie 1 04-24-2008 01:07 PM
how do i change the current prompt in shell? prospekrisal Linux - Newbie 4 07-16-2006 10:35 AM
how can I change the current shell on the fly? Xavius Linux - Newbie 5 04-06-2004 08:00 AM
How to login to bash shell inarin Linux - Newbie 1 10-07-2003 02:45 PM


All times are GMT -5. The time now is 03:40 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration