Ullan
Ullan

Reputation: 1341

How to output a shell command using echo

I am trying to format a shell out

str="set @face_date = '20130612' 

The below code returns the out as '20130612'

echo $str |  cut -d '=' -f2

But the following code is not working

echo "face_date:=TO_DATE("$str | cut -d '=' -f2"),'YYYY/MM/DD');"

Expected Result:

face_date:= to_date('2013-06-12','YYYY/MM/DD');

Thanks in advance for your help

Upvotes: 0

Views: 137

Answers (4)

Sandeep
Sandeep

Reputation: 30

One more way,

echo "face_date:=TO_DATE($(echo $str | cut -d '=' -f2)),'YYYY/MM/DD');"

This will produce following output.

face_date:=TO_DATE( '20130612'),'YYYY/MM/DD');

or you can use

str1=$(echo $str |  cut -d '=' -f2)
echo "face_date:=TO_DATE($(echo ${str1:0:6}-${str1:6:2}-${str1:8:3}),'YYYY/MM/DD');"

for following output

face_date:=TO_DATE('2013-06-12','YYYY/MM/DD');

Upvotes: 0

David C. Rankin
David C. Rankin

Reputation: 84531

You need to either escape the inner double quotes, or surround in $() or ``:

echo "face_date:=TO_DATE($("$str | cut -d '=' -f2")),'YYYY/MM/DD');"

or simply get rid of all the pipes and cut, just do:

echo "face_date:=TO_DATE(${str##* }),'YYYY/MM/DD');"

Upvotes: 0

anubhava
anubhava

Reputation: 784948

You can use:

echo "face_date:=TO_DATE($(cut -d '=' -f2 <<< "$str")),'YYYY/MM/DD');"

OUTPUT:

face_date:=TO_DATE( '20130612'),'YYYY/MM/DD');

Upvotes: 1

Michael
Michael

Reputation: 821

Maybe that can help you. You need to include your command in ` (ASCII #96) in order to get executed. Perhaps that question is a better fit here though.

Upvotes: 0

Related Questions