Skip to content

Commit

Permalink
Make literal "{}" expand to itself
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
faho committed Jan 7, 2018
1 parent 721df61 commit 55ebf44
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
16 changes: 0 additions & 16 deletions doc_src/faq.hdr
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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 -`?

Expand Down
8 changes: 8 additions & 0 deletions doc_src/index.hdr.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions src/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions tests/test1.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test1.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55ebf44

Please sign in to comment.