Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding comma between command line parameter

I want to call a procedure through unix script, it will be generic script so parameters can very. Calling statement will be some something like

<scriptname> <procedure name> <param1> <param2> <param3> <param4>.. so on

What I need is from 2nd command line paramater to last paramter I want all values comma separeted something like this

<param1>,<param2>,<param3>,<param4>

I can do this using a loop, that is from 2nd command line parameter I will itereate each parameter and add comma in it. My question is can I do this with single command?

Note :- Space should be handled properly if present is command line parameter, after last parameter there should not be any comma

like image 249
Pravin Satav Avatar asked Jun 03 '26 18:06

Pravin Satav


1 Answers

"${*:2}" expands to the list of arguments starting at $2, separated by the first character of IFS:

saveIFS=$IFS
IFS=","
args="${*:2}"
IFS=$saveIFS
echo "$args"

Note that this properly preserves spaces within arguments, rather than converting them to commas.

like image 132
Gordon Davisson Avatar answered Jun 06 '26 07:06

Gordon Davisson



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!