Skip to main content
added 3 characters in body
Source Link
Izana
  • 191
  • 1
  • 5

update Tl;dr, use

"'array[*]'""'${array[*]}'" 

To be clear, I don't intend to replicate this answer. I just found there are minor differences to use @ and * to dereference all values from an array.

Under the hood, $* and $@ are all arrays, referring to the argv list.

From the question,

I'm trying to join all of the arguments to a Bash function into one single string with spaces separating each argument.

It has 2 sub-questions:

  1. concatenate the array input arguments into a string.
  2. pass the concatenated string as a single argument for the shell function.

First, concat array into a string,

array=("$@") str="'${array[@]}'" # or str="'${array[*]}'" # or str=\'"${array[*]}"\' 

Second, when you pass str to a function, let's count the number of arguments that function received,

#!/usr/bin/env bash arr=(a b c d) function count_args() { echo '$#' $# } count_args "'${arr[@]}'" count_args \'"${arr[@]}"\' count_args "'${arr[*]}'" count_args \'"${arr[*]}"\' 

output is

$# 4 $# 4 $# 1 $# 1 

only arr[*] wraps the array into 1 argument for the shell function, why?

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using * will group the entire array into one single argument during expansion.

update Tl;dr, use

"'array[*]'" 

To be clear, I don't intend to replicate this answer. I just found there are minor differences to use @ and * to dereference all values from an array.

Under the hood, $* and $@ are all arrays, referring to the argv list.

From the question,

I'm trying to join all of the arguments to a Bash function into one single string with spaces separating each argument.

It has 2 sub-questions:

  1. concatenate the array input arguments into a string.
  2. pass the concatenated string as a single argument for the shell function.

First, concat array into a string,

array=("$@") str="'${array[@]}'" # or str="'${array[*]}'" # or str=\'"${array[*]}"\' 

Second, when you pass str to a function, let's count the number of arguments that function received,

#!/usr/bin/env bash arr=(a b c d) function count_args() { echo '$#' $# } count_args "'${arr[@]}'" count_args \'"${arr[@]}"\' count_args "'${arr[*]}'" count_args \'"${arr[*]}"\' 

output is

$# 4 $# 4 $# 1 $# 1 

only arr[*] wraps the array into 1 argument for the shell function, why?

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using * will group the entire array into one single argument during expansion.

update Tl;dr, use

"'${array[*]}'" 

To be clear, I don't intend to replicate this answer. I just found there are minor differences to use @ and * to dereference all values from an array.

Under the hood, $* and $@ are all arrays, referring to the argv list.

From the question,

I'm trying to join all of the arguments to a Bash function into one single string with spaces separating each argument.

It has 2 sub-questions:

  1. concatenate the array input arguments into a string.
  2. pass the concatenated string as a single argument for the shell function.

First, concat array into a string,

array=("$@") str="'${array[@]}'" # or str="'${array[*]}'" # or str=\'"${array[*]}"\' 

Second, when you pass str to a function, let's count the number of arguments that function received,

#!/usr/bin/env bash arr=(a b c d) function count_args() { echo '$#' $# } count_args "'${arr[@]}'" count_args \'"${arr[@]}"\' count_args "'${arr[*]}'" count_args \'"${arr[*]}"\' 

output is

$# 4 $# 4 $# 1 $# 1 

only arr[*] wraps the array into 1 argument for the shell function, why?

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using * will group the entire array into one single argument during expansion.

added 38 characters in body
Source Link
Izana
  • 191
  • 1
  • 5

update Tl;dr, use

"'array[*]'" 

To be clear, I don't intend to replicate this answer. I just found there are minor differences to use @ and * to dereference all values from an array.

Under the hood, $* and $@ are all arrays, referring to the argv list.

From the question,

I'm trying to join all of the arguments to a Bash function into one single string with spaces separating each argument.

It has 2 sub-questions:

  1. concatenate the array input arguments into a string.
  2. pass the concatenated string as a single argument for the shell function.

First, concat array into a string,

array=("$@") str="'${array[@]}'" # or str="'${array[*]}'" # or str=\'"${array[*]}"\' 

Second, when you pass str to a function, let's count the number of arguments that function received,

#!/usr/bin/env bash arr=(a b c d) function count_args() { echo '$#' $# } count_args "'${arr[@]}'" count_args \'"${arr[@]}"\' count_args "'${arr[*]}'" count_args \'"${arr[*]}"\' 

output is

$# 4 $# 4 $# 1 $# 1 

only arr[*] wraps the array into 1 argument for the shell function, why?

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using * will group the entire array into one single argument during expansion.

update

To be clear, I don't intend to replicate this answer. I just found there are minor differences to use @ and * to dereference all values from an array.

Under the hood, $* and $@ are all arrays, referring to the argv list.

From the question,

I'm trying to join all of the arguments to a Bash function into one single string with spaces separating each argument.

It has 2 sub-questions:

  1. concatenate the array input arguments into a string.
  2. pass the concatenated string as a single argument for the shell function.

First, concat array into a string,

array=("$@") str="'${array[@]}'" # or str="'${array[*]}'" # or str=\'"${array[*]}"\' 

Second, when you pass str to a function, let's count the number of arguments that function received,

#!/usr/bin/env bash arr=(a b c d) function count_args() { echo '$#' $# } count_args "'${arr[@]}'" count_args \'"${arr[@]}"\' count_args "'${arr[*]}'" count_args \'"${arr[*]}"\' 

output is

$# 4 $# 4 $# 1 $# 1 

only arr[*] wraps the array into 1 argument for the shell function, why?

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using * will group the entire array into one single argument during expansion.

update Tl;dr, use

"'array[*]'" 

To be clear, I don't intend to replicate this answer. I just found there are minor differences to use @ and * to dereference all values from an array.

Under the hood, $* and $@ are all arrays, referring to the argv list.

From the question,

I'm trying to join all of the arguments to a Bash function into one single string with spaces separating each argument.

It has 2 sub-questions:

  1. concatenate the array input arguments into a string.
  2. pass the concatenated string as a single argument for the shell function.

First, concat array into a string,

array=("$@") str="'${array[@]}'" # or str="'${array[*]}'" # or str=\'"${array[*]}"\' 

Second, when you pass str to a function, let's count the number of arguments that function received,

#!/usr/bin/env bash arr=(a b c d) function count_args() { echo '$#' $# } count_args "'${arr[@]}'" count_args \'"${arr[@]}"\' count_args "'${arr[*]}'" count_args \'"${arr[*]}"\' 

output is

$# 4 $# 4 $# 1 $# 1 

only arr[*] wraps the array into 1 argument for the shell function, why?

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using * will group the entire array into one single argument during expansion.

deleted 1 character in body
Source Link
Izana
  • 191
  • 1
  • 5

So for your caseupdate

To be clear, I don't intend to replicate this answer. I just found there are minor differences to use @ and * to dereference all values from an array.

Under the hood, $* and $@ are all arrays, referring to the argv list.

From the question,

I'm trying to join all of the arguments to a Bash function into one single string with spaces separating each argument.

It has 2 sub-questions:

  1. concatenate the array input arguments into a string.
  2. pass the concatenated string as a single argument for the shell function.

First, concat array into a string,

array=("$@") str="'${array[@]}'" # or str="'${array[*]}'" # or str=\'"${array[*]}"\' 

Second, when you pass str to a function, let's count the number of arguments that function received,

array=#!/usr/bin/env bash arr=("$@"a b c d) str=\'"$ function count_args() {array[*] echo '$#' $# } count_args "'${arr[@]}'" count_args \'"${arr[@]}"\' count_args "'${arr[*]}'" count_args \'"${arr[*]}"\' 

will work for you if you passoutput is

$# 4 $# 4 $# 1 $# 1 

only strarr[*] to awraps the array into 1 argument for the shell function, it will be treated as one single argument, instead of an array of several arguments.why?

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quotedunless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using ** will group the entire array into one single argument during expansion.

#!/usr/bin/env bash arr=(a b c d) function count_args() { echo '$#' $# echo '$@' $@ echo '$1' $1 } count_args \'"${arr[*]}"\' 

I'm using bash here. Output is,

$# 1 $@ 'a b c d' $1 'a b c d' 

The result shows the number of arguments is just 1.

So for your case,

array=("$@") str=\'"${array[*]}"\' 

will work for you if you pass str to a shell function, it will be treated as one single argument, instead of an array of several arguments.

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using * will group entire array into one single argument during expansion.

#!/usr/bin/env bash arr=(a b c d) function count_args() { echo '$#' $# echo '$@' $@ echo '$1' $1 } count_args \'"${arr[*]}"\' 

I'm using bash here. Output is,

$# 1 $@ 'a b c d' $1 'a b c d' 

The result shows the number of arguments is just 1.

update

To be clear, I don't intend to replicate this answer. I just found there are minor differences to use @ and * to dereference all values from an array.

Under the hood, $* and $@ are all arrays, referring to the argv list.

From the question,

I'm trying to join all of the arguments to a Bash function into one single string with spaces separating each argument.

It has 2 sub-questions:

  1. concatenate the array input arguments into a string.
  2. pass the concatenated string as a single argument for the shell function.

First, concat array into a string,

array=("$@") str="'${array[@]}'" # or str="'${array[*]}'" # or str=\'"${array[*]}"\' 

Second, when you pass str to a function, let's count the number of arguments that function received,

#!/usr/bin/env bash arr=(a b c d)  function count_args() { echo '$#' $# } count_args "'${arr[@]}'" count_args \'"${arr[@]}"\' count_args "'${arr[*]}'" count_args \'"${arr[*]}"\' 

output is

$# 4 $# 4 $# 1 $# 1 

only arr[*] wraps the array into 1 argument for the shell function, why?

Cite from How to use arrays in bash script, which I found it's useful for me,

echo ${array[*]} echo ${array[@]} 

Both syntax let us access all the values of the array and produce the same results, unless the expansion it's quoted. In this case a difference arises: in the first case, when using @, the expansion will result in a word for each element of the array.

Whereas using * will group the entire array into one single argument during expansion.

deleted 1 character in body
Source Link
Izana
  • 191
  • 1
  • 5
Loading
added 216 characters in body
Source Link
Izana
  • 191
  • 1
  • 5
Loading
Source Link
Izana
  • 191
  • 1
  • 5
Loading