home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Unix Power ToolsUnix Power ToolsSearch this book

3.19. Making a "Login" Shell

When you log in to most Unix systems, your shell is a login shell. When a shell is a login shell, it acts differently (Section 3.4).

Sometimes, when you're testing an account or using a window system, you want to start a login shell without logging in. Unix shells act like login shells when they are executed with a name that starts with a dash (-).[11] This is easy to do if you're using a system call in the exec(3) family. These system calls let a C-language programmer give both the filename of an executable file, like sh or /bin/sh, as well as the name that should be used to identify the process (in a ps (Section 24.5) listing, for example), like -sh.

[11]bash also has a command-line option, -login, that makes it act like a login shell. zsh -l (lowercase L) does the same for zsh.

If you're currently using zsh, you can invoke another shell this way by typing a dash and a space before the shell's name:

zsh% - csh
   ...C shell starts, acting like a login shell...

C programmers can write a little program that runs the actual shell but tells the shell that its name starts with a dash. This is how the Unix login process does it:

run_login_csh( )
    execl("/bin/csh", "-csh", 0);

A more general solution is to make a link (Section 10.4) to the shell and give the link a filename starting with a dash. If your own bin subdirectory is on the same filesystem as /bin (or wherever the executable shell file is), you can use a hard link. Otherwise, make a symbolic link, as shown here:

bin Section 7.4, ./- Section 14.13

$ cd $HOME/bin
$ ln -s /bin/csh ./-csh

Then you can execute your new shell by typing its name:

$ -csh
   ...normal C shell login process...
% ...run whatever commands you want...
% logout
$ ...back to original shell

--JP and SJC

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.