LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to change current bash to a login shell (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-change-current-bash-to-a-login-shell-677935/)

jackandking 10-20-2008 09:36 PM

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?

smallville 10-20-2008 10:00 PM

http://www.labtestproject.com/linuxcmd/chsh.html

jackandking 10-20-2008 10:39 PM

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.

Mr. C. 10-21-2008 01:43 AM

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?

jackandking 10-21-2008 04:43 AM

I want to save all my input on cmdline by adding "history -a" to ~/.bash_logout

So, i need a login-shell

Mr. C. 10-21-2008 08:38 AM

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.

jackandking 10-21-2008 09:23 PM

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.

Mr. C. 10-21-2008 09:57 PM

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.


i92guboj 10-22-2008 12:06 AM

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 :p

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 ;)

jackandking 10-22-2008 03:15 AM

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"<--

Mr. C. 10-22-2008 03:24 AM

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.

jackandking 10-22-2008 04:59 AM

finally, I understand, :)
thanks for your patient.

i92guboj 10-22-2008 08:40 AM

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.

Mr. C. 10-22-2008 11:24 AM

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



All times are GMT -5. The time now is 08:42 AM.