3

I wrote a ksh function for git checkout (I've removed some irrelevant proprietary components for the sake of the public question, if you're wondering why it's useful to me):

# Checkout quicker
checkout(){
if [ "$1" == "master" ]; then
   git checkout master
else
   git checkout $1
fi
}

When I look at the function on the command line using functions, though, I get an odd output:

$ functions checkout
checkout()
}

# Checkout quicker
checkout(){
if [ "$1" == "master" ]; then
   git checkout master
else
  $ <- (this is my PS1, which I'm not writing here because it's big)

Why is the function not displaying properly? Did I break functions by technically using the function name inside the function? I am using ksh93u+ 2012-08-01 on RHEL.

7
  • What shell are you using? What is functions? Is that some shell builtin in your shell? And what operating system? Commented Jan 10, 2024 at 14:14
  • @terdon I've added tags with shell and OS. functions is an alias for typeset -f. I thought it was a built-in, maybe not. Commented Jan 10, 2024 at 14:22
  • Can't repro with ksh --version: sh (AT&T Research) 2020.0.0 in Arch Linux Commented Jan 10, 2024 at 14:23
  • @muru I'm on 93u+ 2012-08-01 Commented Jan 10, 2024 at 14:32
  • 1
    I tried a Docker container of centos:7 with ksh-20120801-144.el7_9.armv7hl (ksh --version: sh (AT&T Research) 93u+ 2012-08-01). No luck there either. But in my case it doesn't include the comment before the function. Commented Jan 10, 2024 at 14:50

1 Answer 1

2

Looks like you may have hit the bug later fixed by this commit. I can reproduce it with CentOS 7's ksh93 with:

$ cat a
# Checkout quicker
checkout(){
if [ "$1" == "master" ]; then
   git checkout master
else
   git checkout $1
fi
}
$ . ./a
$ functions checkout
checkout(){
if [ "$1" == "master" ]; then
   git checkout master
else
   git checkout $1
fi
}
$ vi a

Editing the file the function was sourced from, adding some text before the definition of the function. Now:

$ cat a
01234567801234567890123456789
}
# Checkout quicker
checkout(){
if [ "$1" == "master" ]; then
   git checkout master
else
   git checkout $1
fi
}
$ functions checkout
checkout()
}
# Checkout quicker
checkout(){
if [ "$1" == "master" ]; then
   git checkout mast$

Quoting that commit's log:

[v1.1] typeset -f <functname>: generate script using sh_deparse()

Problem reproducer:

  • Locate or write a script with a function definition
  • Source the script with . or source
  • Print the function definition with typeset -f; looks good
  • Edit the file to add or remove characters before or in the function definition, or rename or remove the file
  • Print the function definition with typeset -f again; the output is corrupted!

Cause: ksh remembers the offset and length of the function definition in the source file, and prints function definitions by simply dumping that amount of bytes from that offset in the source file. This will obviously break if the file is edited or (re)moved, so that approach is fundamentally flawed and needs to be replaced.

1
  • Gosh, what a find. Kudos. Commented Jan 10, 2024 at 19:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.