LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2009, 04:13 PM   #1
michiel.vandenbosch
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Rep: Reputation: 0
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
 
Old 02-18-2009, 05:40 PM   #2
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: Fedora 10, CentOS 5.4, Debian 5 Sparc64
Posts: 356

Rep: Reputation: 43
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
 
Old 02-19-2009, 07:01 AM   #3
michiel.vandenbosch
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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..
 
Old 02-19-2009, 07:51 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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";
 
Old 02-19-2009, 03:59 PM   #5
michiel.vandenbosch
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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 :-)
 
Old 02-19-2009, 04:05 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by michiel.vandenbosch View Post
Greetings from little cold and poor belgium :-)
Bedankt :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 08:46 AM
configure bash to echo out the command entered? jrfk2 Linux - General 1 01-18-2007 12:03 PM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM
Bash Scripting - echo command question gbhil Programming 2 04-27-2005 07:27 PM
CGI script problem: Send/Expect command echo on webpage rajatgarg Programming 2 07-11-2004 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration