You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hledger/hledger.m4.md
+37-55Lines changed: 37 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -275,86 +275,78 @@ feel free to skip these until you need them.
275
275
276
276
## Special characters
277
277
278
-
Here we touch on shell escaping/quoting rules, and give some examples.
279
-
This is a slightly complicated topic which you may not need at first,
280
-
but you should be aware of it, so you can return here when needed.
278
+
In commands you type at the command line,
279
+
certain characters have special meaning and sometimes need to be "escaped" or "quoted",
280
+
by prefixing backslashes or enclosing in quotes.
281
281
282
-
If you are able to minimise the use of special characters in your data,
283
-
you won't need escaping as much, and your command lines will be simpler.
284
-
For example, avoiding spaces in account names, and using an ISO-4217 currency code like `USD`
285
-
instead of the `$` currency symbol, can be helpful.
282
+
If you are able to minimise the use of special characters in your data, you won't have to deal with this as much.
283
+
For example, you could use `-` or `_` instead of spaces in account names,
284
+
and you could use the `USD` currency code instead of the `$` currency symbol in amounts.
286
285
287
-
But if you want to use spaced account names and `$`, go right ahead; escaping isn't a big deal.
286
+
But if you prefer to use spaced account names and `$`, it's fine.
287
+
Just be aware of this topic so you can check this doc when needed.
288
+
(Note it is written mainly for unix systems; some details might need to be adapted if you're on Windows.)
288
289
289
290
### Escaping shell special characters
290
291
291
-
At the command line, characters which have special meaning for your shell
292
-
must be "shell-escaped" (AKA "quoted") if you want hledger to see them.
293
-
Often these include space, `<`, `>`, `(`, `)`, `|`, `\`, `$` and/or `%`.
292
+
These are some characters which may have special meaning to your shell (the program which interprets command lines):
294
293
295
-
For example, to match an account name containing the phrase "credit card",
296
-
don't write this:
294
+
- SPACE, `<`, `>`, `(`, `)`, `|`, `\`, `%`
295
+
-`$` if followed by a word character
297
296
297
+
So for example, to match an account name containing spaces, like "credit card", don't write:
298
298
```cli
299
299
$ hledger register credit card
300
300
```
301
301
302
-
In that command, "credit" and "card" are treated as separate query arguments (described below),
303
-
so this would match accounts containing either word.
304
-
Instead, enclose the phrase in double or single quotes:
305
-
302
+
Instead, enclose the name in quotes:
306
303
```cli
307
-
$ hledger register "credit card"
304
+
$ hledger register 'credit card'
308
305
```
309
306
310
-
In Unix shells, writing a backslash before the character can also work. Eg:
307
+
Single quotes are the most reliable. Or use double quotes if you want your shell to treat `$` as a variable interpolation, as in:
308
+
```cli
309
+
$ hledger register "assets:$SOMEACCT"
310
+
```
311
311
312
+
On unix systems (but not Windows), a backslash before the space can also work:
312
313
```cli
313
314
$ hledger register credit\ card
314
315
```
315
316
316
-
Some shell characters still have a special meaning inside double quotes, such as the dollar sign (`$`).
317
-
Eg in `"assets:$account"`, the bash shell would replace `$account` with the value of a shell variable with that name.
318
-
When you don't want that, use single quotes, which escape more strongly:
317
+
On Windows systems, if you are using a Command window rather than Powershell, use double quotes (not single quotes or backslash).
319
318
319
+
Since hledger's query arguments are [regular expressions] (described below), you can also fill that gap with `.` which matches any character:
320
320
```cli
321
-
$ hledger balance 'assets:$account'
321
+
$ hledger register credit.card
322
322
```
323
323
324
-
### Escaping on Windows
325
-
326
-
If you are using hledger in a Powershell or Command window on Microsoft Windows, the escaping rules are different:
327
-
328
-
- In a Powershell window (`powershell`, blue background), you must use double quotes or single quotes (not backslash).
329
-
- In a Command window (`cmd`, black background), you must use double quotes (not single quotes or backslash).
330
-
331
-
The next two sections were written for Unix-like shells, so might need to be adapted if you're using `cmd` or `powershell`. (Edits welcome.)
332
324
333
325
### Escaping regular expression special characters
334
326
335
-
Many hledger arguments are [regular expressions] (described below), and these too have characters which cause special effects.
336
-
Some of those characters are `.`, `^`, `$`, `[`, `]`, `(`, `)`, `|`, and `\`.
337
-
When you don't want these to cause special effects, you can "regex-escape" them by writing `\` (a backslash) before them.
338
-
But since backslash is also special to the shell, you may need to also shell-escape the backslashes.
339
-
Note that `$` only has special meaning to the shell if followed by a word character indicating a variable name.
340
-
When appearing alone at the end of a word as in `cur:\\$` the dollar sign is not considered special and does not *have* ta be escaped.
327
+
Some characters also have special meaning in [regular expressions], which hledger's arguments often are. Those include:
341
328
342
-
Eg, in the bash shell, to match a literal `$` sign, you could write:
329
+
-`.`, `^`, `$`, `[`, `]`, `(`, `)`, `|`, `\`
343
330
331
+
To escape one of these, write `\` before it.
332
+
But note this is in addition to the shell escaping above.
333
+
So for characters which are special to both shell and regular expressions, like `\` and `$`, you will sometimes need two levels of escaping.
334
+
335
+
For example, a balance report that uses a `cur:` query restricting it to just the $ currency, should be written like this:
344
336
```cli
345
337
$ hledger balance cur:\\$
346
338
```
339
+
Explanation:
340
+
1. Add a backslash `\` before the dollar sign `$` to protect it from regular expressions (so it will be matched literally with no special meaning).
341
+
2. Add another backslash before that backslash, to protect it from the shell (so the shell won't consume it).
342
+
3.`$` doesn't need to be protected from the shell in this case, because it's not followed by a word character; but it would be harmless to do so.
347
343
348
-
The escape here is to pass through a literal escape, the dollar sign does not have to be escaped because it is at the end of a word and hence can't be interpreted as a shell variable.
349
-
350
-
Alternatively, use single quotes to avoid the special meanings of shell characters and see more visually what value will be passed through:
351
-
344
+
But here's another way to write that, which tends to be easier:
345
+
add backslashes to escape from regular expressions, then enclose with quotes to escape from the shell:
352
346
```cli
353
-
$ hledger balance 'cur:\$'
347
+
$ hledger balance cur:'\$'
354
348
```
355
349
356
-
Here hledger will get a query string of `\$`, which is a regex escaped and hence will match a literal dollar sign.
357
-
358
350
### Escaping in other situations
359
351
360
352
hledger options and arguments are sometimes used in places other than the command line, with different escaping rules.
@@ -373,16 +365,6 @@ For example, backslash-quoting generally does not work there. Here are some more
373
365
[argument file]: #argument-files
374
366
[config file]: #config-file
375
367
376
-
### Using a wild card
377
-
378
-
When escaping a special character is too much hassle (or impossible), you can often just write `.` (period) instead.
379
-
In regular expressions, this means "accept any character here".
380
-
Eg:
381
-
382
-
```cli
383
-
$ hledger register credit.card
384
-
```
385
-
386
368
## Unicode characters
387
369
388
370
hledger is expected to handle non-ascii characters correctly:
0 commit comments