bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Some minor notes on manual chapter 4 "Shell Builtin Commands"


From: Robert Elz
Subject: Re: Some minor notes on manual chapter 4 "Shell Builtin Commands"
Date: Mon, 09 Oct 2023 23:25:51 +0700

    Date:        Mon, 9 Oct 2023 10:35:20 -0400
    From:        Chet Ramey <chet.ramey@case.edu>
    Message-ID:  <a9e4d016-de50-4b67-a688-590ff0a77c13@case.edu>

  | There is surprising variance in behavior here, from a status of 2 to 1
  | to 0 (dash), plus the silly ksh "substitute old for new in $PWD," which
  | the NetBSD sh (!) also performs.

Yes, we do...  it is surprisingly useful behaviour, and has been there
for a long time (Nov 2002, so it isn't going away) but it doesn't really
need to be in the shell, the following cd replacement func (which I use
in bash, and other shells that don't support this) adds good enough
support for it (not perfect, it doesn't always do the expected thing
if the same arg string occurs multiple times in $PWD - that's very rare):

cd() {
        local P=

        case "$#" in
        1)      ;;
        2)      case "${PWD}" in
                ("$1"*) set -- "${2}${PWD#"${1}"}";;
                (*"$1") set -- "${PWD%"${1}"}${2}";;
                (*"$1"*) set -- "${PWD%%"${1}"*}${2}${PWD#*"${1}"}";;
                (*)     printf >&2 '%s\n' "cd: no '$1' in '$PWD'"; return 1;;
                esac
                P=printf
        esac

        enter "$@" || return $?
        ${P:+printf '%s\n' "${PWD}"}
}

enter() is just:

enter() {
        command cd -P -- "$@"
}

which I use all over the place, as I am unable to fathom a use for cd
without -P being supplied, that one line could replace the use of enter
in the cd func.

The cd func is assuming that no flags are ever needed (which is true for
me as the -P is always being added later...) so if there are 2 args, it
must be a 'substitution' use of cd.

kre

ps: This is useful when you're working with lots of source trees in parallel,
like /release/8/long/path/to/sources/being/examined and /release/9/... and
/release/10/...   so one can just do "cd 8 9" or "cd 8 10" (etc) to switch
from one tree to the other.   A lot of what ksh added is nonsense, but this
one isn't (other than that it doesn't really need to be built into the shell).





reply via email to

[Prev in Thread] Current Thread [Next in Thread]