dmc
dmc

Reputation: 411

bash dir string construction from command in one line

dir=$(pwd; echo '/hi/'; date "+%Y_%m_%d-%H_%M_%S")

echo $dir

gives

/some/path/hear /hi/ 2014_11_30-12_40_17

Why are there spaces ? how to make this path in one line with out spaces ?

Thanks

Upvotes: 1

Views: 122

Answers (4)

tvCa
tvCa

Reputation: 816

This still works as well:

$ dir=`pwd`/hi/$(date '+%Y_%m_%d-%H_%M_%S')
$ echo $dir
/home/oracle/hi/2014_11_30-14_18_30
$

Funny there are so many ways of doing this

Upvotes: 1

Etan Reisner
Etan Reisner

Reputation: 80931

Use printf.

$ printf '%s/hi/%s\n' "$(pwd)" "$(date "+%Y_%m_%d-%H_%M_%S")"
/home/path/hi/2014_11_30-07_53_05

Upvotes: 0

Wintermute
Wintermute

Reputation: 44023

Easiest way, I think:

dir="$(pwd)/hi/$(date '+%Y_%m_%d-%H_%M_%S')"

the $() expressions work just like normal variables in this context.

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174696

You could use sed.

$ dir=$(pwd; echo '/hi/'; date "+%Y_%m_%d-%H_%M_%S")
$ dir=$(sed 's/ //g' <<< $dir)
$ echo $dir
/home/path/hi/2014_11_30-18_21_09

Upvotes: 1

Related Questions