Using Here-Document and Command Substitution is the standard way to go in this case:
grep '^[a-zA-Z.:]' <<IN \ | sed -r '/^(rm|cd)/d' \ | uniq -u \ > "$filepath" $(cat -- "$filepath") IN For other questions, they were explained in many questions before: