Article

← Retour au blog

🇫🇷 Lire en français

Linux bash enigma: ls |wc

2008-06-19 #835 · rév. 1 · EN

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

I am analyzing incoming ad 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 to answer a question, and then display the result of the grep.

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

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

I was expecting the following results:
<pre> How many files in this folder: ls |wc 21 37 535 </pre>

Unfortunately, I got the following:
<pre> How many files in this folder: ls |wc ls: |wc: No such file or directory </pre>

Turning on the debug mode, I got the following:
<pre> +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 </pre>

It appears that bash adds single quotes where it finds blank spaces. How come?

Any idea how to fix this?