$*
is equivalent to $1 $2 $3...
- split on all spaces.
"$*"
is equivalent to "$1 $2 $3..."
- no splitting here.
"$@"
is equivalent to "$1" "$2" "$3"...
- split on arguments (every argument is quoted individually).
How to quote $(command)
so that it treats output lines of the command in the same way "$@"
treats arguments?
The problem I want to solve:
I have a backup function that takes files by arguments and backups each of them (e.g.: backup file1 file_2 "file 3"
). I want to quickly backup files that are returned by another command.
mycmd
returns three files (one per line): file1, file_2 and file 3 (containing a space). If I ran the following command:
backup $(mycmd)
it would be equivalent to running backup file1 file_2 file 3
and it would result in an error because of non-existing files file and 3. However running it this way:
backup "$(mycmd)"
is equivalent to run:
backup "file1 file_2 file 3"
None of them is good enough.
How can I use command substitution to get a call equivalent to: backup "file1" "file_2" "file 3"
?
Currently my only workaround is:
while read line; do backup "$line"; done < <(mycmd)