How to dry up repeated flags in bash scripts using wrapper functions

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-11-23

I was working on a bash script to get WordPress plugins installed. There was a lot of error-prone repetition, as follows:

wp plugin install --activate wordpress-seo --allow-root --path=/usr/src/html
wp plugin activate advanced-custom-fields-pro --allow-root --path=/usr/src/html
wp plugin install --activate acf-content-analysis-for-yoast-seo --allow-root --path=/usr/src/html

What was the best way to remove it? Answer: create a transparent wrapper function that includes those arguments:

wp() {
  /usr/local/bin/wp --allow-root --path=/usr/src/html "$@"
}

wp plugin install wordpress-seo
wp plugin install advanced-custom-fields-pro