Home > other >  Parameter expansion with replacement, avoid additional variable
Parameter expansion with replacement, avoid additional variable

Time:11-05

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.

CodePudding user response:

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

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