Skip to main content
eliminate the unneeded space
Source Link
x-yuri
  • 3.5k
  • 11
  • 40
  • 66

Most of the time you want $@. But a use I've found... Let's say you're creating a script that creates another script (e.g. to be executed on a remote server). You have an array with commands, and you want to pass $@ to the command:

f() {
    cmds+=("some-command ${@@Q}")
}
f a b c

But this way 3 elements are added to the array: some-command a, b, and c. What you want is:

fcmds+=()"some-command ${*@Q}")
    

Or even:

cmds+=("some-commandcommand${@+ ${*@Q}}")
}
f a b c

Most of the time you want $@. But a use I've found... Let's say you're creating a script that creates another script (e.g. to be executed on a remote server). You have an array with commands, and you want to pass $@ to the command:

f() {
    cmds+=("some-command ${@@Q}")
}
f a b c

But this way 3 elements are added to the array: some-command a, b, and c. What you want is:

f() {
    cmds+=("some-command ${*@Q}")
}
f a b c

Most of the time you want $@. But a use I've found... Let's say you're creating a script that creates another script (e.g. to be executed on a remote server). You have an array with commands, and you want to pass $@ to the command:

f() {
    cmds+=("some-command ${@@Q}")
}
f a b c

But this way 3 elements are added to the array: some-command a, b, and c. What you want is:

cmds+=("some-command ${*@Q}")

Or even:

cmds+=("some-command${@+ ${*@Q}}")
Source Link
x-yuri
  • 3.5k
  • 11
  • 40
  • 66

Most of the time you want $@. But a use I've found... Let's say you're creating a script that creates another script (e.g. to be executed on a remote server). You have an array with commands, and you want to pass $@ to the command:

f() {
    cmds+=("some-command ${@@Q}")
}
f a b c

But this way 3 elements are added to the array: some-command a, b, and c. What you want is:

f() {
    cmds+=("some-command ${*@Q}")
}
f a b c