Reputation: 5245
I have a Bash script which (in a simplified form) does this:
#!/bin/bash
function ag_search_and_replace {
ag -l "$1" "${@:3}" | xargs -r perl -i -pe "s/$1/$2/g"
}
locations="$@"
ag_search_and_replace search replace $locations
This works as expected when the argument have no spaces, e.g.:
my_script foo bar
however, when there are spaces, e.g.:
my_script foo "ba r"
the script fails.
Is there a simple way to process arguments with spaces?
Upvotes: 1
Views: 52
Reputation: 530872
"$@"
is the way to do it, but you lose the benefits when you unnecessarily assign it to a regular variable.
ag_search_and_replace search replace "$@"
If you must create a new named variable, use an array.
locations=( "$@" )
ag_search_and_replace search replace "${locations[@]}"
Upvotes: 4