I'm trying to write a command that's a one-line for loop containing a command that uses the & operator to put the command in the background:
for dir in $(ls); do command $dir &; done
Where & tells BASH to run command in the background. Yet BASH returns
bash: syntax error near unexpected token `;'
What's going on here? How can I use & in conjunction with ;? I tried enclosing things in quotes, like do "command $dir &" or do command $dir "&" but those have unexpected results.
$(ls)in place of*is generally considered a bad practice. Because of the nature of globbing*will allow you to deal with filenames that contain whitespace,$(ls)will not. Consider a list of files, "one a", "one b", "two". With$(ls)you will iterate "one", "a" "one", "b", "two". With*you will iterate the actual file list.