Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle whitespaces in arguments to a bash script

I am having issues handling arguments that contain white spaces in a my bash script.

The script

#!/bin/bash
for i in $*
do
    echo "$i"
done

The call (with 2 arguments)

$ ./script.sh "a b" "c"

The actual output (as if there were 3 arguments)

a
b
c

The expected output (as if there were 2 arguments)

a b
c

Can someone explain how to get the expected output?

like image 532
Max Avatar asked Aug 13 '26 09:08

Max


1 Answers

Change $* to "$@" on the first line.

like image 159
Flash Avatar answered Aug 15 '26 01:08

Flash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!