[D-community-offtopic] how to quote the pipe symbol in shell scripts?

Kamaraju S Kusumanchi raju.mailinglists at gmail.com
Wed May 25 03:59:47 UTC 2011


Consider the following script

$cat quote_pipe_symbol.sh                                                                                                               
#! /bin/sh

command="tail -n 3 cols.txt | cut -f 2-3 -d ','"
echo $command
$command


$cat cols.txt                                                                                                                           
11, 12, 13, 14, 15,
21, 22, 23, 24, 25,
31, 32, 33, 34, 35,
41, 42, 43, 44, 45,
51, 52, 53, 54, 55,


I want to echo the command and then execute it. But I am having trouble 
quoting the pipe symbol ('|') appropriately. When the above script is 
executed, I get

$./quote_pipe_symbol.sh                                                                                                                 
tail -n 3 cols.txt | cut -f 2-3 -d ','
tail: invalid option -- 'd'
Try `tail --help' for more information.


I tried adding the -x option to see what's happening.

$bash -x ./quote_pipe_symbol.sh                                                                                                         
+ command='tail -n 3 cols.txt | cut -f 2-3 -d '\'','\'''
+ echo tail -n 3 cols.txt '|' cut -f 2-3 -d ''\'','\'''
tail -n 3 cols.txt | cut -f 2-3 -d ','
+ tail -n 3 cols.txt '|' cut -f 2-3 -d ''\'','\'''
tail: invalid option -- 'd'
Try `tail --help' for more information.

It looks like the pipe symbol is being embedded in single quotes. But I do 
not know how to suppress this behaviour. Any ideas/comments/suggestions?

thanks
raju
-- 
Kamaraju S Kusumanchi
http://malayamaarutham.blogspot.com/




More information about the D-community-offtopic mailing list