shell - bash scripting: build a command then execute -


i'm trying make function takes in arguments f hello there friend , searches directory using find occurences of of strings, find | grep 'hello\|there\|my\|friend'. i'm new shell scripting, code below:

function f {    cmd="find | grep '"   var in "$@"        cmd="$cmd$var\\|"   done   cmd="${cmd%\\|}'"   echo "$cmd"   $cmd  } 

when execute command, this:

# f hello there friend find | grep 'hello\|there\|my\|friend' find: `|': no such file or directory find: `grep': no such file or directory find: `\'hello\\|there\\|my\\|friend\'': no such file or directory 

why not work, , how can make work? imagine it's string not being converted command, don't know enough how shell scripting works figure out.

don't put entire command in string; build argument grep

f () {    local grep_arg=''   delim=''   var in "$@";       grep_arg+="$delim$var"       delim='\|'   done   echo "find | grep '$grep_arg'"   find | grep "$grep_arg" } 

Comments