Search code examples
bashparameter-expansion

Parameter expansion with replacement, avoid additional variable


I'm trying to join input $* which is one parameter consisting of all the parameters added together.

This works.

#!/bin/bash

foo() {
    params="${*}"
    echo "${params//[[:space:]]/-}"
}

foo 1 2 3 4
1-2-3-4

However, is it possible to skip the assignment of variable?

"${"${*}"//[[:space:]]/-}"

I'm getting bad substitution error.

I can also do

: "${*}"
echo "${_//[[:space:]]/-}"

But it feels hacky.


Solution

  • One option could be to set 's internal field separator, IFS, to - locally and just echo "$*":

    foo() {
        local IFS=$'-'
        echo "$*"
    }