Advertisement

Backquote question

Started by March 04, 2009 02:21 PM
10 comments, last by Oxyd 15 years, 7 months ago
Hey guys, I've just got a quick question. I have used unix before, but no so in depth as the class I'm taking. This is probably a basic question, but I am wondering what enclosing a command in backquotes (`) achieves. I've searched on the Internet for a bit, but I'm quite drowsy and am probably not understanding it well. I just read in my notes that it "allows you to insert the output of commands into the command line itself, thus allowing you to generate commands automatically". Does this mean if I had this command: `echo grep blah blah.txt` It would first run echo, output the command "grep blah blah.txt" and then execute that grep command? If this is the case, what would this be useful for? Cheers for any help, Ray
Yes, that's what it does. It's great for use in scripts:

FILES=`ls -1 *.txt`for FILE in FILES; do    # do something clever with FILEdone

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Advertisement
Thanks a lot Sander, appreciate it.

Ray
Might as well keep it in the same thread, as it's related.

I understand the backquotes now, but there is one statement I'm finding hard to digest.

echo ` ls ~ ` | wc -l

The output it gives me is 1. To figure out why I tried just echo ` ls ~ ` and it printed the home directory but in one line. Makes sense.

But from my understanding, backquotes executes the output of the command within them, hence why I get an error when trying: ` ls ~ ` since the first filename isn't a UNIX command. Why then instead of the usual ls where it prints out each file on a separate line, is it printing the home directory files in one line?

I just can't get my head around it.

Thanks for any help guys.

Ray
backticks execute the command within them. They don't execute the output of the command within them. A command in backticks becomes a string containing the output.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Note that an alternative to backticks is
$(commands)
which comes in handy sometimes as it can be nested. E.g.:
vi $(which printer-config)

(where printer-config is a little private script)
Advertisement
Quote: Original post by raydey
echo ` ls ~ ` | wc -l

The output it gives me is 1. To figure out why I tried just echo ` ls ~ ` and it printed the home directory but in one line. Makes sense.


Let's see:
[u-pl6] ~ > ls ~BIG  Backup  Desktop  WWW  documents  mail  matika  notes  pascal  soubory  temp  vmware  wcx_ftp.ini  wincmd.ini
So I've got those files in my home, the output fits on one line -- good.

[u-pl6] ~ > echo `ls ~`BIG Backup Desktop WWW documents mail matika notes pascal soubory temp vmware wcx_ftp.ini wincmd.ini
The `ls ~` part got replaced by the actual ls output, so it's actually equivalent to
[u-pl6] ~ > echo BIG  Backup  Desktop  WWW  documents  mail  matika  notes  pascal  soubory  temp  vmware  wcx_ftp.ini  wincmd.iniBIG Backup Desktop WWW documents mail matika notes pascal soubory temp vmware wcx_ftp.ini wincmd.ini
That's kinda meaningless, but still one line, so if you count the lines via wc -l you get 1.

Now, if you try sole `ls ~` on the shell line, you get an error:
[u-pl6] ~ > `ls ~`zsh: command not found: BIG
That's because what you did is effectively equivalent to doing
[u-pl6] ~ > BIG  Backup  Desktop  WWW  documents  mail  matika  notes  pascal  soubory  temp  vmware  wcx_ftp.ini  wincmd.ini
This is obiously not a valid command.
Quote: That's kinda meaningless, but still one line, so if you count the lines via wc -l you get 1.


How about `ls -1` then? :-)

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

You might as well write ls | wc -l [smile]

I commonly use grep foobar `find . -name '*.php'` as a find-in-files operation.
Quote: Original post by Sander
How about `ls -1` then? :-)

Definitely much more useful. [smile]

Quote: Original post by ToohrVyk
I commonly use grep foobar `find . -name '*.php'` as a find-in-files operation.

Does that have any advantage over find . -name '*.php' -exec grep foobar {} ';'?

This topic is closed to new replies.

Advertisement