cajwine
cajwine

Reputation: 3160

Like variable substitution but on the result of an command in bash

I can do this

datestr=$(date)
echo "${datestr// /_}"

prints date where all spaces are replaced with _

Fri_Sep__5_21:56:05_CEST_2014

Is it possible to do somewhat without the helper variable? something such

echo ${$(date)// /_}  #this of course didn't works

I know, it is possible to do with e.g. echo $(date | tr ' ' '_'), but this runs another process and i looking for a pure bash.

Upvotes: 1

Views: 62

Answers (2)

Cyrus
Cyrus

Reputation: 88583

Another idea. Create a function

x() { echo "${@// /_}"; }

and enter

x $(date)

Without a helper variable, no onother process and pure bash.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

No, it is not. The first operand of a parameter expansion operation cannot be another expansion nor a substitution.

Upvotes: 4

Related Questions