1

Sorry for the silly question here today.

I am passing a PHP array to a bash script using implode();.

To test, I am echoing the implode and I can see all array items there, but when I printf '%s\n' "${files[@]}" only the first element of the array is printed.

Am I missing something?

Here is more info:

PHP:

$files = $_POST['files'];    
$files2 = implode(" ", $files);
echo $files2   ## I can see full output here. 
shell_exec ("./sequential.sh $files2");

Bash:

files = $1
printf '%s\n' "${files[@]}" >> mytempfile.txt 

Thanks for any guidance.

1
  • $files2 should be string and not an array Commented Mar 21, 2017 at 16:02

1 Answer 1

1

files = $1

$1 is only the first argument. If you want all arguments then you can find them in $@:

printf '%s\n' "$@" >> mytempfile.txt
Sign up to request clarification or add additional context in comments.

7 Comments

Ok, I tested that and got pretty close. It did print the full contents. I suppose I should have included this in my post but I edited out a lot of the script. I am passing 4 other variables from PHP to bash, so if I use $@ it prints ALL the variables, but I only need whats in the array. So now to figure that out.
$@ contains all the parameters passed to the shell script in the command line.
I marked yours correct as it was correct for how I posed the question, but I think I need to figure it out further to get it handling all the parameters correctly. Thanks for your help.
@fcamp Pass the other 4 arguments first, then use files=({@:5}") to make files an array containing the arguments starting with $5.
@GordonDavisson the syntax is off a bit in your example, but it got me exactly where I needed to go! It should be files=${@:5}. This works.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.