How to avoid ambiguous redirect error in Bash ?

Hello ,

Today I was trying to take Threaddump for my Java process in single command with date and hostname like this

jstack -l `pgrep java` > /tmp/TD_`hostname`_`date`_`pgrep java`

But I am getting error like 

-bash: /tmp/TD_`hostname`_`date`_`pgrep java`: ambiguous redirect

Then I posted at  unix.stackexchange.com and a friend named Muru helped me in fixing that issue with very simple way


$ echo foo > /tmp/TD_`hostname`_`date`_`pgrep java`
bash: /tmp/TD_`hostname`_`date`_`pgrep java`: ambiguous redirect
$ echo foo > /tmp/"TD_`hostname`_`date`_`pgrep java`"
$ ls /tmp/TD*

/tmp/TD_muru-arch_Sat 26 Dec 16:14:29 IST 2015

Summary : Command jstack -`pgrep java` > /tmp/TD_`hostname`_`date`_`pgrep java` is wrong 

and right way of using this is jstack -`pgrep java` > /tmp/"TD_`hostname`_`date`_`pgrep java` "


Hope it will help you. 

0 Comments