Diff like a boss! (custom Bash commands)

Source code


<span style="color:#323232;">#!/usr/bin/env bash
</span><span style="color:#323232;">
</span><span style="color:#323232;"># diffc - diff commands
</span><span style="color:#323232;"># - allows to call as: diffc 'command one' 'command two'
</span><span style="color:#323232;">#   instead of:        diff  &lt;(command one) &lt;(command two)
</span><span style="color:#323232;">#   (Just to save typing a few characters. Lol I'm a lazy programmer)
</span><span style="color:#323232;">function diffc () {
</span><span style="color:#323232;">  if [[ "$#" != "2" ]]; then
</span><span style="color:#323232;">    echo "diffc requires two arguments"
</span><span style="color:#323232;">    return 1
</span><span style="color:#323232;">  fi
</span><span style="color:#323232;">
</span><span style="color:#323232;">  local command=$(printf 'diff &lt;( %s ) &lt;( %s )' "$1" "$2")
</span><span style="color:#323232;">  echo $command
</span><span style="color:#323232;">  eval $command
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;"># diffh - diff history
</span><span style="color:#323232;"># - make a diff of the output of the last two commands in the shell history
</span><span style="color:#323232;">function diffh () {
</span><span style="color:#323232;">  # first one is 2nd to last. second is last
</span><span style="color:#323232;">  # remove preceeding spaces
</span><span style="color:#323232;">  local first=$(fc -ln -2 -2)
</span><span style="color:#323232;">  local second=$(fc -ln -1 -1)
</span><span style="color:#323232;">
</span><span style="color:#323232;">  # print and run diff
</span><span style="color:#323232;">  local command=$(printf 'diff &lt;( %s ) &lt;( %s )' "${first}" "${second}")
</span><span style="color:#323232;">  echo $command
</span><span style="color:#323232;">  eval $command
</span><span style="color:#323232;">
</span><span style="color:#323232;">  local error_code=$?
</span><span style="color:#323232;">
</span><span style="color:#323232;">  # replace this 'diffh' entry in history with the command 
</span><span style="color:#323232;">  history -s "$(echo $command)"
</span><span style="color:#323232;">  
</span><span style="color:#323232;">  return $error_code
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span>
bloopernova,
@bloopernova@programming.dev avatar

Difftastic is an amazing diff tool.

github.com/Wilfred/difftastic

cvf,

Looks very nice, thanks!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • [email protected]
  • aaaaaaacccccccce
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • oklahoma
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • SuperSentai
  • feritale
  • KamenRider
  • All magazines