LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with the echo -command in BASH (https://www.linuxquestions.org/questions/programming-9/problem-with-the-echo-command-in-bash-705708/)

michiel.vandenbosch 02-18-2009 04:13 PM

Problem with the echo -command in BASH
 
Hi there, I need something like this:

echo "SOME TEXT `tail -1 `overvieuwFunction` | cut -d" " -f$1` SOME MORE TEXT";

So the next line:
tail -1 `overvieuwFunction` | cut -d" " -f$1
I need in an echo. But there are probably to much " and '

Thanks for your help

TBC Cosmo 02-18-2009 05:40 PM

Are you looking for this?
Code:

#!/bin/bash
#
echo "SOME TEXT \`tail -1 \`overvieuwFunction\` | cut -d\" \" -f\$1\` SOME MORE TEXT"

Code:

$ ./t.sh
SOME TEXT `tail -1 `overvieuwFunction` | cut -d" " -f$1` SOME MORE TEXT


michiel.vandenbosch 02-19-2009 07:01 AM

No, I will explain it like this.

# echo "TEXTA";
RESULT: TEXTA

But now I want to have a bash-command (like for instance the whoami bash-command) within the echo:
# echo "TEXTB `whoami` TEXTC";
RESULT: TEXTB root TEXTC

So these two examples work, but now I want to use a bash-command which needs arguments with f.i. " and '

f.i. the next line I want instead of the whoami bash-command in the example above (the overvieuwFunction is a function I have written, and the tail-command uses the output from this function as its input):
tail -1 `overvieuwFunction` |cut -d" " -f1

so I want to really use this line in the echo from above.

Thanks and I hope its now clear to you..

Hko 02-19-2009 07:51 AM

Does this part work?
Code:

tail -1 `overvieuwFunction`
It does not look correct to me...

Anyway, the problem you're asking about is that you try to use: `some-command` nested within another set of ` and `. That doesn't work.

If you use the bash shell, it can be solved by using $(some-command) syntax. This does the same as the back-tocks (``), but does allow nesting.
Code:

echo "TEXTB $(tail -1 $(overvieuwFunction) |cut -d" " -f1) TEXTC";

michiel.vandenbosch 02-19-2009 03:59 PM

Yes, it has to be:
overvieuwFunction | tail -1 | cut -d" " -f$1

And not:
tail -1 `overvieuwFunction` | cut -d" " -f$1

By the way, thanks for the $() solution :-)

Greetings from little cold and poor belgium :-)

Hko 02-19-2009 04:05 PM

Quote:

Originally Posted by michiel.vandenbosch (Post 3450202)
Greetings from little cold and poor belgium :-)

Bedankt :-)


All times are GMT -5. The time now is 12:53 PM.