Make literal "{}" expand to itself

This caused major annoyances with e.g. `find -exec`, and it's utterly
useless - "{}" expands to nothing, so why have it at all?

Fixes #1109.
This commit is contained in:
Fabian Homborg 2018-01-02 15:20:19 +01:00
parent 721df61f4b
commit 55ebf4430f
6 changed files with 27 additions and 16 deletions

View file

@ -9,6 +9,7 @@ This section is for changes merged to the `major` branch that are not also merge
- `read` now requires at least one var name (#4220).
- `set x[1] x[2] a b` is no longer valid syntax (#4236).
- For loop control variables are no longer local to the for block (#1935).
- A literal `{}` now expands to itself, rather than nothing. This makes working with `find -exec` easier. (#1109, #4632)
## Notable fixes and improvements
- `wait` builtin is added for waiting on processes (#4498).

View file

@ -24,7 +24,6 @@
- <a href='#faq-titlebar'>I'm seeing weird output before each prompt when using screen. What's wrong?</a>
- <a href='#faq-greeting'>How do I change the greeting message?</a>
- <a href='#faq-history'>Why doesn't history substitution ("!$" etc.) work?</a>
- <a href='#faq-find-braces'>Why do I get a missing argument error with `find ... {}`?</a>
- <a href='#faq-cd-minus'>How can I use `-` as a shortcut for `cd -`?</a>
- <a href='#faq-uninstalling'>How do I uninstall fish?</a>
- <a href='#faq-third-party'>Where can I find extra tools for fish?</a>
@ -244,21 +243,6 @@ Fish history recall is very simple yet effective:
See <a href='index.html#editor'>documentation</a> for more details about line editing in fish.
<hr>
\section faq-find-braces Why do I get a missing argument error with `find ... {}`?
Running `find ... -exec ... {}` produces an error:
find: missing argument to '-exec'
The problem is caused by the empty braces, which are subject to <a href="index.html#expand-brace">brace expansion</a>.
Quote the empty braces to achieve the desired effect:
\fish{cli-dark}
find ... -exec ... '{{}}'
\endfish
<hr>
\section faq-cd-minus How can I use `-` as a shortcut for `cd -`?

View file

@ -506,6 +506,14 @@ mv *.{c,h} src/
# Moves all files with the suffix '.c' or '.h' to the subdirectory src.
\endfish
A literal "{}" will not be used as a brace expansion:
\fish
echo foo-{}
# Outputs foo-{}
echo foo-{$undefinedvar}
# Output is an empty line - see <a href="#cartesian-product">the cartesian product section</a>
\subsection expand-variable Variable expansion

View file

@ -980,6 +980,15 @@ static expand_error_t expand_brackets(const wcstring &instr, expand_flags_t flag
}
}
// Expand a literal "{}" to itself because it is useless otherwise,
// and this eases e.g. `find -exec {}`. See #1109.
if (bracket_begin + 1 == bracket_end) {
wcstring newstr = instr;
newstr.at(bracket_begin - in) = L'{';
newstr.at(bracket_end - in) = L'}';
return expand_brackets(newstr, flags, out, errors);
}
if (syntax_error) {
append_syntax_error(errors, SOURCE_LOCATION_UNKNOWN, _(L"Mismatched brackets"));
return EXPAND_ERROR;

View file

@ -15,6 +15,10 @@ logmsg Bracket expansion
echo x-{1}
echo x-{1,2}
echo foo-{1,2{3,4}}
echo foo-{} # literal "{}" expands to itself
echo foo-{{},{}} # the inner "{}" expand to themselves, the outer pair expands normally.
echo foo-{""} # still expands to foo-
echo foo-{$undefinedvar} # still expands to nothing
logmsg Escaped newlines
echo foo\ bar

View file

@ -11,6 +11,11 @@
x-1
x-1 x-2
foo-1 foo-23 foo-24
foo-{}
foo-{} foo-{}
foo-
foo- foo- foo- foo-
####################
# Escaped newlines