You are here

Terminal Geek

Setting a User's Default Login Shell

When I became a Mac "switcher" in 1996, it took me a while to get accustomed to the GUI. My mind kept searching for the CLI "under the hood", so to speak. Gradually I came to understand the new paradigm, but now with OS X we've come full circle.

So, of couse, "Terminal" is one of my favorite apps. It's not ideal for everything, but better for some things. For example, I often use ssh to a sonnet to a remote machine on which the Finder is unresponsive, poke around, and usually fix it or at least reboot it cleanly.

The first thing you have to do is choose a shell. Or not, since Mac OS X chooses one for you. Having been a Sun/BSD/Solaris user, the C Shell was my original preference, and I was most familiar with its syntax. That worked out fine since tcsh, an enhanced version of csh, was the default shell up through 10.2 "Jaguar".

Along came 10.3, "Panther", and the Bourne Shell (Bourne Again, actually) became the default. I customized all my machines to use the "C Shell", but my natural curiosity nudged me to compare the differences.

Now I'm not going to get into any flame wars about whether Bourne is better than Korn (the shell, not the band). Suffice to say there are several available for you to explore. The options under 10.4 "Tiger" are *sh* (the original), *ksh* (Korn Shell), *csh* (Original C shell), and *zsh* (ummm... The "Z" shell?)

To change a user's default login shell, type the command "chpass -s ". For example,

% chpass -s /bin/bash

Valid shells for your system can be found by type the following command:

% cat /etc/shells

Issuing the "chpass" command with no arguments will allow you to interactively edit information such as the users full name and contact information.

UPDATE: With the introduction of Mac OS X 10.5 "Leopard", Apple has completely abandoned NetInfo. Procedures in this post which refer to using the netinfo suite of commands (niutil, etc) are obsolete as of this release. Ironically, the default shell with Leopard is tcsh, so we're back to where we were with "Jaguar" (10.2).

Setting the Default Shell Using NetInfo

(NOTE: NetInfo is no longer available as of OS X 10.5)

I eventually came to prefer bash, and that's still what I mostly use. To choose a different shell for Terminal, you can run NetInfo (in /Applications/Utilities). You'll need to authenticate, then select the "users" record. Find your user name, then scroll down in the properties window until you see "shell". It will be set to "/bin/bash" (in 10.3 or later). Change that to "/bin/tcsh". Of course, there's an easier way, using the Terminal...

$ sudo niutil -createprop . /users/shortname shell /bin/bash

Replace "shortname" with your short user name, generally the same as the name of your home folder under /Users. You may notice that I used all lowercase in the command above. This works because Mac OS X "folds" case in file names and paths. It's not truly case sensitive, so /usr/bin/head is the same as /usr/bin/HEAD. This can cause problems with certain shell scripts and applications, notably [Perl], which uses case extensively to differentiate modules. But that's for another post.

PATH

This environment variable contains a list of directories to search for executables when you type a command that is not recognized as a built-in shell command. You can check your path by typing the following in bash:

$ echo $PATH

The output of this command will look something like this:

/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/games

or you can do:

$ set | grep PATH

or in the c-shell:

% setenv | grep PATH

The "|" is the pipe operator which redirects the output of one command to the input of another. "grep" is a search program which scans the standard input for the string specified. The output of this command will look something like this:

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/games

If you want to add to the path, say, if you have a "bin" in your home directory, you would type the following:

PATH=$PATH:$HOME/bin

or in the c-shell:

% setenv PATH {$PATH}:{$HOME}/bin

The new path would remain in effect until you exit the current shell. To make it permanent, you would add the command to either .bash_profile or .tcshrc file.

The defaults Command

Many share/freeware apps for Mac OS X simply provide GUI front ends for Terminal applications. Nothing wrong with this, but if you're like me, you want to know what's going on behind the scenes, so to speak. From the "man" page:

Defaults allows users to read, write, and delete Mac OS X
user defaults from a command-line shell.

If you don't know what a "man page" is, then open a terminal shell and type "man man". Really. The gist of it is that "man" is short for "manual", and is an on-line documentation for shell utilities, programming libraries and functions, etc.

Anyway, here's how to set the Finder to display all files on your machine, including the senstive system files and hidden files which you really shouldn't mess around with.

$ defaults write com.apple.finder AppleShowAllFiles -bool TRUE

Now the man page for defaults says you shouldn't change the settings of any running application, but that's rather unavoidable when twiddling with the Finder's settings. So after the above command, you'll have to relaunch the Finder (Cmd+Opt+Esc). Voila!

This is useful to view the hidden disk images on your system install CDs and DVDs. Most of the Apple installer discs contain a folder called ".images". As you probably know by now, Unix (and Mac OS X) treats filenames that begin with a dot as hidden, and won't display them by default. You can view hidden files in the finder by typing

$ ls -a

The -a option instructs the ls (list files) command to display all files, including hidden ones like .htaccess, .images, ., .. and so on.

Here is the defaults command to make those same files invisible again:

defaults write com.apple.finder AppleShowAllFiles -bool FALSE

pbcopy & pbpaste

These terminal commands allow the shell to interact with the system pasteboard or clipboard. This is the same temporary storage space used when you use the Cut/Copy/Paste commands from the GUI. The pbcopy command takes stdin and places it in the pastboard. The pbpaste command sends the pasteboard to stdout. For more information, refer to the man pages.

Tags: