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.
- 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 - Enter
`./test-shebang`, thenM-C-e. This works as expected, expanding the line tohi.C-_works normally, undoing the expansion. - Enter
`./test-no-shebang`, thenM-C-e. This works strangely, indeed expanding the line tohi, but removing the prompt in front of the line.C-_does not work, printing the character literally instead, and nor do most bindings, butC-ukind of works, successfully clearing the line (but not clearing thehi).
Why is this the case? Can I make shell-expand-line work for scripts without shebang?
EDIT:
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).
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 bash3.2.57(1)-release.
M-C-emean? How would I invoke that?set -o emacs(EMACS mode command-line editing), so I suggest mentioning that explicitly in your question.shell_expand_linefrom the file bashline.c are 1. the new functionexpand_string_dollar_quoteis called fromshell_expand_line, 2. the functionexpand_wordis used in 5.2.32 instead of the code callingexpand_stringin 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.