1

I have these bash functions

filter_history(){
  grep -v '^cd[^ ]|^ls[^ ]'
}

get_clean_bash_history(){
  cat "$HOME/.bash_history" | filter_history
}

I am trying to get the bash history but filter out boring commands like cd and ls...but my grep expression doesn't work, does anyone know why?

1

Looks like the -E flag was necessary to use with grep, this appears to work:

grep -v -E '^cd +|^ls +'

but please correct me if this is not sufficient or fails in edge cases

|improve this answer|||||
  • Alternative without -E and a lot of escaping: grep -v '^\(cd\|ls\) \+' – Freddy May 13 '19 at 20:29
1

You just need to escape the or:

function get_clean_bash_history(){
    grep -v '^cd \|^ls ' "$HOME/.bash_history"
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.