Let's say I want to execute several commands with similar flags such as:du -sh / --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4tar cvf archive / --exclude=dir1 --exclude=dir2 --exclude=dir3/dir4I want to reuse the sequence of --exclude flags and make the code more clean by e.g. saving these flags to a variable.So afterwards above commands look likedu -sh / $EFLAGStar cvf archive / $EFLAGSNote that tar excludes files and directories relative to the given directory.What is the best and cleanest solution?BackgroundI want to backup my system using tar and exclude some directories that I don't need.
Reuse similar flags for multiple commands
bash;shell;variable
You can try this:$ echo -e / --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4/ --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4$ echo !!:3-$echo --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4--exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4Here we use bash history expansion:!! refer to the last command3-$ refer to the 3rd parameter to the lastYou should turn on bash history expansion features in your script, which are disabled in non interactive shell:set -o historyset -o histexpand