22
submitted 1 day ago* (last edited 1 day ago) by thingsiplay@beehaw.org to c/linux@lemmy.ml

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.

Edit 1:

After some suggestions from the comments, here is a little shorter version. sort | uniq can be replaced by sort -u, as the output of them should be identical in this case (in certain circumstances they can have different effect!). Also someone pointed out my useless cat, as the file can be used directly with awk. And for good reason. :D Enjoy, and thanks for all.

type -P $(awk '{print $1}' ~/.bash_history | sort -u) | sort

I still have no real use case for this one liner, its mainly just for fun.

top 12 comments
sorted by: hot top controversial new old
[-] pemptago@lemmy.ml 4 points 1 day ago

As I've been working on an install script for making my setup more portable, this is handy and timely. Thanks for sharing!

PS I hate to be the UUOC person. I'm sure you're already aware and it was a deliberate choice.

[-] thingsiplay@beehaw.org 2 points 1 day ago

PS I hate to be the UUOC person. I’m sure you’re already aware and it was a deliberate choice.

I wish it was. I honestly forgot. yeah, shame on me. :D Before this, at the position of cat there was actually a different command, which I replaced with this. And I didn't think of adding the file to awk instead. I'll update the line with this suggestion and a suggestion from someone else.

[-] lauha@lemmy.one 4 points 1 day ago

Can this one liner determine if program halts too?

[-] TechieDamien@lemmy.ml 2 points 1 day ago

That's impossible, look up the Halting problem

[-] lauha@lemmy.one 1 points 20 hours ago

That's the joke

[-] thingsiplay@beehaw.org 3 points 1 day ago

I'm actually not sure what you mean by that. This script will only list the programs you used in the terminal. It prints the fullpath of each command. That's all it does.

Do you want know if a program is currently running?

[-] lauha@lemmy.one 3 points 1 day ago

Silly me misunderstood the intention of the script. I imagined it lists the programmatic paths the program can take, like all the decision in the program. :)

[-] hexagonwin@lemmy.sdf.org 3 points 1 day ago

sort | uniq can be sort -u instead btw

[-] thingsiplay@beehaw.org 2 points 1 day ago

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

[-] hexagonwin@lemmy.sdf.org 2 points 1 day ago

Interesting, I never knew. Thanks

[-] non_burglar@lemmy.world 1 points 1 day ago

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.

[-] just_another_person@lemmy.world -4 points 1 day ago

Or just use zsh 🫰

this post was submitted on 15 Jun 2025
22 points (95.8% liked)

Linux

55319 readers
762 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS