Mac OS X Cmdline

From HerzbubeWiki
Jump to: navigation, search

Terminal.app preferences

Colors

The colors in the following list are given as decimal RGB values.

  • Cursor: 85, 85, 85 (dark gray)
  • Normal text: 0, 0, 0 (black)
  • Selected text: 170, 170, 170 (light gray)
  • Bold text: 22, 227, 255 (cyan)
  • Background: 128, 133, 176 (dark lilac)


bash

Startup files

A login shell (regardless of whether or not it is interactive) reads/executes the following startup files:

    • /etc/profile
    • the first of the following files that exists
      • ~/.bash_profile
      • ~/.bash_login
      • ~/.profile

An interactive shell that is not a login shell (e.g. the shell started by Terminal.app) reads/executes the following startup files:

  • ~/.bashrc

A non-interactive shell that is not a login shell (e.g. to run a shell script, also during cron execution) reads/executes the following startup files:

  • the file listed by the environment variable BASH_ENV

This page does not discuss the bash behaviour, that is even more different, when bash is

  • invoked with the name sh
  • started with --posix
  • run by the remote shell daemon (e.g. rshd
  • run with an effective user/group id different from the real user/group id


Shell environment

Some things (e.g. environment variables) are required in all environments, regardless of whether it is an interactive, non-interactive, a login or a non-login environment. Other things (e.g. aliases) are required only under certain circumstances. I therefore create and fill several files in

/usr/local/etc

each file containing a certain kind of thing (e.g. aliases) so that I can reference the file and its definitions from where I need it.

/usr/local/etc/setalias   # shell aliases
/usr/local/etc/setenv     # various shell commands that fit in nowhere else
/usr/local/etc/setfunct   # shell functions
/usr/local/etc/setvar     # environment variables
/usr/local/etc/linked.bashrc    # users can create a symbolic link to this file as ~/.bashrc
/usr/local/etc/linked.profile   # users can create a symbolic link to this file as ~/.profile

~/.profile

tharbad:~ --> ls -l .profile
lrwxr-xr-x   1 admin  staff  29 Sep  5  2004 .profile -> /usr/local/etc/linked.profile

tharbad:~ --> cat .profile
#!/bin/bash
. /sw/bin/init.sh         # do ***not*** source this in .bashrc
. /usr/local/etc/setvar
. $DIRETC/setenv
. $DIRETC/setfunct
. $DIRETC/linked.bashrc   # Wird von bash als Login-Shell nicht gelesen


~/.bashrc

tharbad:~ --> ls -l .bashrc
lrwxr-xr-x   1 admin  staff  28 Sep  5  2004 .bashrc -> /usr/local/etc/linked.bashrc
tharbad:~ --> cat .bashrc
#!/bin/bash

# Needs to be checked to prevent multiple inclusion
if [ -z "$SETVAR_INCLUDED" ]
then
  . /usr/local/etc/setvar
fi

# CTRL-d should not terminate shell
case $SHELL in
  /bin/sh|/bin/bash|/bin/ksh)
    set -o ignoreeof
    ;;
esac

# Aliases need to be included for every new shell because they cannot
# be exported
. $DIRETC/setalias

# Command line prompt
PS1="\h:\w --> "
if [ "$(id -u)" = "0" ]; then
  PS1="ROOT $PS1"
fi
export PS1

# Turn off auto-correct feature of the 'cd' command when a directory
# is mis-spelled
shopt -u cdspell


Hostname

The PS1 variable used in the above example of .bashrc uses the escaped character \h, which expands to the hostname (as printed by the hostname command) up to the first ".". This is not necessarily the hostname as it has been set in the System Preferences, under the "Sharing" preference pane. When googling you will find many pages that claim to know how Mac OS X sets up its hostname, but so far this page has been the most convincing source. It claims that the OS goes through a list of possibilities and chooses the first valid hostname that can be found:

  1. The name provided by the DHCP or BootP server for the primary IP address
  2. The first name returned by a reverse DNS (address-to-name) query for the primary IP address
  3. The local hostname (set in the Sharing pane of System Preferences)
  4. The name localhost

My current experience with Mac OS X 10.5 and my private LAN setup is this:

  • The reverse DNS lookup definitely works. While I didn't have my own DNS server, I always got a hostname such as "192.168.1.9.local.home". As I found out now, this is the result of a reverse DNS lookup with my ISP's DNS server
  • I have tried setting a different hostname in the DHCP configuration ("option host-name <foo>") but had no success, Mac OS X 10.5 still chose the name that a reverse DNS lookup yielded.
  • Ditto for setting a different hostname in the Sharing pane of System Preferences - the reverse DNS lookup still was the name-giver

Other notes:

  • Setting a hostname with the hostname command works only until the next reboot
  • The hostname in the Sharing pane of the System Preferences is not automatically set to the hostname of the command line environment.
  • The Sharing hostname, by the way, is stored in /Library/Preferences/SystemConfiguration/preferences.plist


zsh

After switching to macOS 14 and the default shell "zsh", I found that quite a few things were different compared to "bash", although zsh is labeled as "bash compatible".

Initialization files

  • Instead of ~/.profile and ~/.bashrc (bash) the zsh uses the files ~/.zprofile and ~/.zshrc.

Key bindings

  • Various key bindings that I am used from bash do not work as expected. Examples: Ctrl+A / Ctrl+E going to the beginning / end of the line, or Ctrl+R to search through the command history.
  • It seems that zsh sets its key bindings based on the value of the EDITOR or VISUAL envvars. In my case EDITOR was set to vim which causes the keymap "viins" to be selected.
  • Apparently what I am used to is the keymap "emacs". This can be set with the command bindkey -e. The setting is not inherited by sub-shells, to be available it therefore must be executed in ~/.zshrc.
  • I have added corresponding handling to my HTB script collection.
  • More options about the bindkey built-in can be found in the documentation of the zsh line editor (zshle).


Locale

Terminal.app preferences

In the "Emulation" tab:

  • Nicht-ASCII-Zeichen in Escape-Sequenz umwandeln = true

In the "Darstellung" tab

  • Zeichensatz-Codierung = Unicode (UTF-8)


Keyboard input

Configure ~/.inputrc (possibly /usr/local/etc/linked.inputrc, making a symlink to it in your home directory)

192:~ --> cat .inputrc 

# ------------------------------------------------------------
# The following settings influence 8-bit character
# processing, e.g. Umlaute. The comments were taken
# from the man page of bash
# ------------------------------------------------------------
# If set to "on", readline converts 8-bit characters
# to 7-bit characters (i.e. remove the 8th bit) and
# prefix the character with an escacpe character
set convert-meta off

# If set to "on", readline enables 8-bit input
# (i.e. it doesn't strip the 8th bit from the input)
# regardless of what the terminal claims it can
# support
set input-meta on

# If set to "on", readline displays 8-bit characters
# directly instead of as an escape sequence
set output-meta on


Environment

Various commands installed through fink sometimes print helpful information. That information expects some kind of locale to be set. Do this by setting the LANG environment variable to some useful value. I set the variable in /usr/local/etc/setvar

LANG=de_CH.UTF-8

To get all available locales:

locale -a


vi

Add to ~/.vimrc

syntax enable
set encoding=utf-8