build command by concatenating string in bash
I have a bash script that builds a command-line in a string based on some parameters before executing it in one go. The parts that are concatenated to the command string are supposed to be separated by pipes to facilitate a "streaming" of data through each component.
A very simplified example:
#!/bin/bash
part1=gzip -c
part2=some_other_command
cmd="cat infile"
if [ ! "$part1" = "" ]
then
cmd+=" | $part1"
fi
if [ ! "$part2" = "" ]
then
cmd+=" | $part2"
fi
cmd+="> outfile"
#show command. It looks ok
echo $cmd
#run the command. fails with pipes
$cmd
For some reason the pipes don't seem to work. When I run this script i get different error messages relating usually to the first part of the command (before the first pipe).
So my question is whether or not it is possible to build a command in this way, and what is the best way to do it?
It all depends on when things get evaluated. When you type $cmd
, the whole rest of the line is passed as arguments to the first word in $cmd
.
walt@spong:~(0)$ a="cat /etc/passwd"
walt@spong:~(0)$ b="| wc -l"
walt@spong:~(0)$ c="$a $b"
walt@spong:~(0)$ echo $c
cat /etc/passwd | wc -l
walt@spong:~(0)$ $c
cat: invalid option -- 'l'
Try 'cat --help' for more information.
walt@spong:~(1)$ eval $c
62
walt@spong:~(0)$ a="echo /etc/passwd"
walt@spong:~(0)$ c="$a $b"
walt@spong:~(0)$ echo $c
echo /etc/passwd | wc -l
walt@spong:~(0)$ $c
/etc/passwd | wc -l
walt@spong:~(0)$ $c |od -bc
0000000 057 145 164 143 057 160 141 163 163 167 144 040 174 040 167 143
/ e t c / p a s s w d | w c
0000020 040 055 154 012
- l \n
0000024
walt@spong:~(0)$ eval $c
1
This shows that the arguments passed to the echo
command are: "/etc/passwd
", "|
" (the vertical bar character), "wc
" and "-l
".
From man bash
:
eval [arg ...]
The args are read and concatenated together into
a single command. This command is then read and
executed by the shell, and its exit status is returned
as the value of eval. If there are no args, or only null
arguments, eval returns 0.
One solution to this, for future reference, is to use "eval". This ensures that whatever way the string is interpreted by bash is forgotten and the whole thing is read as if it was typed directly in a shell (which is exactly what we want).
So in the example above, replacing
$cmd
with
eval $cmd
solved it.
@waltinator already explained why this does not work as you expected. Another way around it is to use bash -c
to execute your command:
$ comm="cat /etc/passwd"
$ comm+="| wc -l"
$ $comm
cat: invalid option -- 'l'
Try 'cat --help' for more information.
$ bash -c "$comm"
51