1

Earlier versions of the Korn shell (ksh) allowed aliases to be exported to subshells by using the -x option, for example:

alias -x dog=cat

With the current version of macOS the ksh man page says:

"The obsolete -x option has no effect."

Is there an alternate way to export aliases in macOS in ksh without putting them in .kshrc?

1
  • How are you defining them in the first place, other than in $ENV (~/.kshrc by default) ? The -x option to alias was removed back in 93g (default in macOS is 93u+) according to the RELEASE93 file. (note to mods... this isn't macOS-specific and probably should be moved). Commented Nov 7, 2021 at 16:52

2 Answers 2

2

As a general rule, put idempotent things (e.g., setting the PATH environment variable) in ~/.profile, but put things that must be performed on a per-shell/subshell basis (e.g., aliases; HISTFILE and ENV and PS1 environment variables) in ~/.kshrc (or whatever your ${ENV} is set as in ~/.profile). Then you will have your aliases proper set afresh in each subshell of ksh.

~/.profile

if [ -n "$BASH_VERSION" ]
    then
    if [ -f "$HOME/.bashrc" ]
        then
        . "$HOME/.bashrc"
        fi
elif [ -n "$KSH_VERSION" ]
    then
    if [ -f "$HOME/.kshrc" ]
        then
        . "$HOME/.kshrc"
        fi
elif [ -n "$ZSH_VERSION" ]
    then
    if [ -f "$HOME/.zshrc" ]
        then
        . "$HOME/.zshrc"
        fi
    fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ]
    then
    PATH="$HOME/bin:$PATH"
    fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ]
    then
    PATH="$HOME/.local/bin:$PATH"
    fi

~/.kshrc

hostname=/bin/hostname
ksh=/bin/ksh
stty=/bin/stty
tput=/usr/bin/tput
tty=/usr/bin/tty
whoami=whoami
bold=$(${tput}      bold)
dim=$(${tput}       dim)
bgblue=$(${tput}    setab 4)
bgcyan=$(${tput}    setab 6)
bggreen=$(${tput}   setab 2)
bgmagenta=$(${tput} setab 5)
bgred=$(${tput}     setab 1)
bgwhite=$(${tput}   setab 7)
bgyellow=$(${tput}  setab 3)
fgblue=$(${tput}    setaf 4)
fgcyan=$(${tput}    setaf 6)
fggreen=$(${tput}   setaf 2)
fgmagenta=$(${tput} setaf 5)
fgred=$(${tput}     setaf 1)
fgwhite=$(${tput}   setaf 7)
fgyellow=$(${tput}  setaf 3)
sgr0=$(${tput}      sgr0)
smso=$(${tput}      smso)
smul=$(${tput}      smul)
rmso=$(${tput}      rmso)
rmul=$(${tput}      rmul)
tty_path=$(${tty})
set -o emacs
export PS1='${fgblack}${bgyellow}↑$
${sgr0}ksh${dim}${fgblue}${bggreen}$(${hostname})${sgr0}${fgwhite}${tty_path#/dev/}${sgr0}${smul}${dim}$(${whoami})${sgr0}${PWD}
$ '
export ENV=~/.kshrc
alias l='ls -AFq'
alias rd='rmdir'
alias md='mkdir'
alias ssh='ssh -X'
alias emacs-nw='emacs -nw'
alias over='_(){ cd ../$1; }; _'
alias cd..='_(){ cd ../$1; }; _'

This best practice is true of shells other than ksh as well, such as csh and bash and zsh.

(Most especially don't put incremental/nonidempotent appending or prepending of PATH environment variable in ~/.kshrc, but that is unrelated per se to the question here.)

1

I am not much of a ksh user so some other users will probably be able to improve on this answer. (Or disapprove.)

While I have not found how to export aliases, I have found how to save the aliases to an exported variable. The script in the .kshrc file can be modified to recreate the aliases stored in this variable. The proposed commands to add to the .kshrc file are shown below

if [[ ${#LISTOFALIASES} != 0 ]]; then
    ALIASIFSBACKUP="$IFS"
    IFS=$'\n'
    while read -r CURRENTALIAS; do
        eval "command -p alias $CURRENTALIAS"
    done <<< "$LISTOFALIASES"
    IFS="$ALIASIFSBACKUP"
    unset ALIASIFSBACKUP CURRENTALIAS
fi
export LISTOFALIASES
aliasx() { command -p alias "$@"; LISTOFALIASES="$(command -p alias)"; }
unaliasx() { command -p unalias "$@"; LISTOFALIASES="$(command -p alias)"; }
command -p alias alias=aliasx
command -p alias unalias=unaliasx

 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .