It only works with the first command in the recorded history, not with any sub shells or chained commands.
#!/usr/bin/env bash
# 1. history and $HISTFILE do not work in scripts. Therefore cat with a direct
# path is needed.
# 2. awk gets the first part of the command name.
# 3. List is then sorted and duplicate entries are removed.
# 4. type -P will expand command names to paths, similar to which. But it will
# also expand aliases and functions.
# 5. Final output is then sorted again.
type -P $(cat ~/.bash_history | awk '{print $1}' | sort | uniq) | sort
After reading a blog post, I had this script in mind to see if its possible. This is just for fun and I don’t have an actual use for it. Maybe some parts of it might inspire you to do something too. So have fun.
They are not exactly the same. I always default to piping it, because I never remember which to use when. And had to lookup again to make sure I was not hallucinating: https://unix.stackexchange.com/questions/76049/what-is-the-difference-between-sort-u-and-sort-uniq/76095#76095
Interesting, I never knew. Thanks
I agree they aren’t the same, especially if you need uniq to count things.
However, be aware that pipes can be a real problem in scripts because of globbing and expansion.