Alexander Zeitler
Alexander Zeitler

Reputation: 13109

Pass alias from chained command to unalias

I have a list of alias definitions in a file I want to unalias in a batch.

The file looks like this:

please=sudo
po='git push origin'

I have come this far but I'm not sure how to pass the alias names to the unalias com

cat old.txt | cut -d = -f 1

Upvotes: 2

Views: 46

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295650

To allow the input file to contain comments, you might do something like:

while IFS== read -r name val; do
  [[ $val ]] || continue # skip any line that didn't have a "="
  [[ $name =~ [#] ]] && continue # skip any line that had a # anywhere before the "="
  unalias "$name"
done <old.txt

This avoids relying on any tools external to the shell itself -- all processing is done with bash-native logic. (Sometimes this is the right thing, sometimes it's not -- bash's string processing tends to be slower than general-purpose tools, but those tools typically also have significant startup-time costs, making them undesirable to run in a loop).

  • The while read idiom is documented in BashFAQ #1. Setting IFS== means that we split into fields when an = is seen; providing name and val means that the first field goes into name, and all subsequent fields go into val.
  • [[ $var =~ $regex ]] does POSIX ERE-style regex matching.
  • ... <in.txt is both more efficient than cat in.txt | ..., and avoids triggering the issues described in BashFAQ #24 (which can happen when piping data into a loop).

Upvotes: 4

Alexander Zeitler
Alexander Zeitler

Reputation: 13109

This did the trick:

unalias $(cat old.txt | cut -d = -f 1)

Upvotes: 0

Related Questions