Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: history 6 -> history | tail -n 6 #626

Open
onlyphantom opened this issue Aug 1, 2021 · 3 comments
Open

Discussion: history 6 -> history | tail -n 6 #626

onlyphantom opened this issue Aug 1, 2021 · 3 comments
Labels
discussion discussion before a proposal

Comments

@onlyphantom
Copy link
Contributor

In Chapter 4.3 Turning Interactive Work into a Script, the author suggests using history 6 to retrieve / show the last 6 commands in our history:

For example, we can save the last six commands to summarize_all_books.sh:

$ history 6 > summarize_all_books.sh
$ cat summarize_all_books.sh

297 for x in ../data/*.txt; do echo $x;
  bash book_summary.sh $x Author; done > authors.txt
298 for x in ../data/*.txt; do echo $x;
  bash book_summary.sh $x Release; done > releases.txt
299 ls
300 mkdir ../results
301 mv authors.txt releases.txt ../results
302 history 6 > summarize_all_books.sh

history 6 is probably specific to specific flavours of Unix, and is not guaranteed to work. I propose to change it to history | tail -n 6 instead:

For example, we can save the last six commands to summarize_all_books.sh:

$ history | tail -n 6 > summarize_all_books.sh
$ cat summarize_all_books.sh
  1. This is also consistent with the book's previous chapters (specifically in 3.8.12).
  2. Much like head -6 may work in some flavours of Unix, head -n 6 is recommended in the book.

What do everyone think?

I'm not sure if this is a welcomed suggestion, so I didn't send in a PR but decided to open an issue instead, and maybe get some feedback as to what the authors think. If this is the wrong avenue, apologies in advance 🙏

Thank you for the work you put into this.

@onlyphantom onlyphantom added the discussion discussion before a proposal label Aug 1, 2021
@joelostblom
Copy link
Contributor

Thanks for you for opening this issue @onlyphantom ! I believe history [n] is built into bash across all platforms as document here. Did you encounter a situation where it would not work for you with bash?

As for head, I think it is part of gnucoreutils rather than a bash builtin and head -6 is an older syntax that is still supported (last paragraph on this page). I am not sure if this is portable across OSes, but I find it more likely for non-builtin programs so it is possible. I generally advocate for the shorter syntaxes since I think that makes people more likely to use them (as long as the brevity does not come at great expense of clarity).

In either case, I agree we should be consistent but I would lean towards changing 3.8.12 to the briefer syntax. Let's see what others think.

@onlyphantom
Copy link
Contributor Author

Thank you for that reassurance!

As a Mac user at home (macOS recently went from bash to zsh), I can confirm that history [n] did not work for zsh.

The expected output of history [n] was n last records of history, equivalent to history | tail -n [n]. But in zsh:

  • history [n] does the same thing as history. n is ignored.
  • history -[n] does work; history -5 returns the last 5 results of history.

history -[n] is consistent with head -[n] and tail -[n], so I think if that works across all platforms, I'd see how it makes sense to propose that instead.

@joelostblom
Copy link
Contributor

joelostblom commented Aug 2, 2021

It is not working in zsh is because history is a builtin command in bash with its own functionality and options interface, whereas in zsh it is an alias for fc -l, which does not support the same flags as the bash history command (man zshbuiltins to read more). I suppose this is made as a convenience for people switching between shells but might end up creating confusing situations like this.

Unfortunately, history -[n] does not work in bash regardless of the underlying OS and I am not sure there is a syntax that works in both zsh and bash here. It might be worth adding a note for the zsh equivalent for some of the bash commands that don't work the same in zsh (not sure if this is the only one), especially for people on macOS who might not realize that Apple somewhat recently switched the default. I opened #627 to see what others think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion discussion before a proposal
Projects
None yet
Development

No branches or pull requests

2 participants