1

In bash, the very useful shell-expand-line (M-C-e) expands command substitutions to their contents, on the current line. However, this works very strangely for commands without shebangs.

One may verify this by the following.

  1. Create a simple executable file with and without shebang.
echo echo hi >> test-no-shebang chmod +x test-no-shebang echo '#!/bin/sh' >> test-shebang echo echo hi >> test-shebang chmod +x test-shebang 
  1. Enter `./test-shebang`, then M-C-e. This works as expected, expanding the line to hi. C-_ works normally, undoing the expansion.
  2. Enter `./test-no-shebang`, then M-C-e. This works strangely, indeed expanding the line to hi, but removing the prompt in front of the line. C-_ does not work, printing the character literally instead, and nor do most bindings, but C-u kind of works, successfully clearing the line (but not clearing the hi).

Why is this the case? Can I make shell-expand-line work for scripts without shebang?

EDIT:

  1. Some comments thought that it would be wise for me to mention that the mentioned key-bindings refer to the emacs line-editing bindings (which are the default bash key-bindings).

  2. With more testing, I observe that I can reproduce the problem on the mac Terminal and on kitty on bash version 5.2.15(1)-release, but cannot reproduce the problem on bash 3.2.57(1)-release.

9
  • What does M-C-e mean? How would I invoke that? Commented Aug 4, 2024 at 9:16
  • 2
    Cannot reproduce but you should always use a shebang on all scripts. Commented Aug 4, 2024 at 11:18
  • 3
    The default assumption by readers of your question will not be that you're using set -o emacs (EMACS mode command-line editing), so I suggest mentioning that explicitly in your question. Commented Aug 4, 2024 at 17:11
  • 1
    @SottoVoce I can add it to the question, but it would be strange to me that anyone would assume that I'm not using emacs line-editing, since its the default. Commented Aug 4, 2024 at 21:45
  • 2
    I suggest reporting this as a bug to the appropriate mailing list. The only two things different between the listed versions of Bash in the function shell_expand_line from the file bashline.c are 1. the new function expand_string_dollar_quote is called from shell_expand_line, 2. the function expand_word is used in 5.2.32 instead of the code calling expand_string in 3.2.57, which is retained in the file but commented out. My guess is there is some kind of unwanted interaction of new functions with Readline. Commented Aug 5, 2024 at 7:25

1 Answer 1

1

Gratitude to @Vilinkameni for proposing to consult the mailing list; the wizards of GNU swiftly offered a diagnosis of this issue as a known bug, already fixed in the devel branch. Presumably, this will be fixed for versions above 5.2.

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.