4 ms·
The very first example (echo "Hello $NAME!") won’t work if `histexpand` is set, which I believe is the default.
by egwynn 8y ago
The very first example (echo "Hello $NAME!") won’t work if `histexpand` is set, which I believe is the default.
- ben509 8y agoIt works if the ! is quoted, as in the example.
- egwynn 8y agoThat is incorrect. From the man page: If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash.
- ben509 8y agoThe example works; it must be because a single ! doesn't expand to anything.
- egwynn 8y agoLooks like this is an artifact of the built-in (old) bash on osx. I see that it works in the newer versions of bash on my freebsd and linux hosts. The mac bash man page says: Only backslash (\) and single quotes can quote the history expansion character. where the newer man page says: Only backslash (\) and single quotes can quote the history expansion character, but the history expansion character is also treated as quoted if it immediately precedes the closing double quote in a double-quoted string. Thanks for pointing this out!
- ben509 8y agoSure, thanks for checking the man page! I usually just rip out bangs because I can never remember the rules for history expansion.
- deleted 8y ago[deleted]
- blueflow 8y agoAlso, '< file.txt | ...' does not work as well. Not sure if its well researched.
- egwynn 8y agoYeah my eyebrow went up at that one too…
- ben509 8y agoWhich is surprising since "research" here means pasting it into a terminal... Maybe they meant `cat file.txt | while ...`? Even then, you never want to do that when you're reading because elements of a pipeline are run in subshells. The usual idiom is: while cond; do thing done < input > output It's also the problem with cheatsheets: the underlying idea in bash is that compound statements are like mini programs, so they have file handles that can be redirected. That doesn't come through a cheatsheet very well.