I have an alias created:
alias my_rsync = "rsync -av ${PATH_EXCLUDE_DEV} ${PATH_SYS_DEV}/` ${PATH_SYS_SANDBOX}/ && wait $! && cd - &>/dev/null\"
When is load this alias and watch it with the command 'type my_rsync' I see that $! is gone because it has been interpreted.
Normally I do escape with backslash and it does function well. For example:
alias my_rsync = "mysql ${DB_DATA_SYS_SANDBOX} -e 'SHOW TABLES' | grep -v 'Tables_in_${DB_NAME_SANDBOX}' | while read a; do mysql ${DB_DATA_SYS_SANDBOX} -e \"DROP TABLE \$a\";done"
Can you guys give me a hint? Thanks.
Use a function, not an alias, and you avoid this altogether.
...in this formulation, no expansions will ever happen until the function is expanded.
As for
wait
-- it only makes sense at all when you're running things in the background. The usage you have here doesn't start anything in the background, so thewait
calls have no purpose.On the other hand, the following shows some wait calls that do have purpose:
This runs multiple rsyncs (one for each host) at the same time in the background, stores their PIDs in an array, and then iterates through that array when they're all running to let them complete.
Notably, it's the single
&
operator that causes thersync
s to be run in the background. If they were separated from the following command with&&
,;
or a newline instead, they would be run one at a time, and the value of$!
would never be changed.