Linux bash enigma: ls |wc

June. 19th, 2008 17:59 by sdelucaPermalink | TrackBack: http://stephanedeluca.com/trackback/835 — exists for 10 years & 6 months ago

While working on an administrative shell script for our mobile location-based social networking service http://m.mobiluck.com I came accross a weird issue. Let me explain what I want first.

I am analysing incoming ads hits on our service, data mining the apache log file. I wanted to use a simple script in bash.

The point is to display how I grep the file for answering a question, and then display the result of the grep.

But as I came accross an issue, I will generalize it with this very simple script:

#!/bin/bash
c='ls |wc'; 
echo How many files in this folder:
echo "$c"; 
`$c`

I was expecting the following results:

How many files in this folder:
ls |wc 
      21      37     535

Unfortunately, I've got the following:

How many files in this folder:
ls |wc
ls: |wc: No such file or directory

Turning on the debug mode I've got the following:

+echo How many files in this folder:
How many files in this folder:
+ c='ls |wc'
+ echo 'ls |wc'
ls |wc
++ ls '|wc'
ls: |wc: No such file or directory

It appears that bash add simple quotes where it finds blank spaces. How it comes?

Any idea how to fix this?