Skip to content
Advertisement

ksh/bash run command line from variable

I set in cmd variable the find syntax: , I use ksh shell/bash

 # cmd=" find /usr/cti/conf -name "*.tgz*" "
 # echo $cmd
   find /usr/cti/conf/ -name "*.tgz*"

so why when I want to run the cmd as the following I not actually activate the find …

 # exec $cmd
  appserver1a:/var/tmp/    ROOT #     ( this exit from the shell )

it’s also not works when I run with double brackets

  exec "$cmd"
  ksh:  find /usr/cti/conf/backup -name "*.tgz*" :  not found

what is the resolution for this?

Remark I not want to set the cmd like this ( this is works )

     cmd=`  find /usr/cti/conf/backup -name "*.tgz*"  `

or

      cmd=$( find /usr/cti/conf/backup -name "*.tgz*" )

Advertisement

Answer

Store your command string in the variable:

cmd="find /usr/cti/conf/backup -name "*.tgz*""

Then, evaluate the variable contents:

eval "$cmd"

UPDATE: the safer option, according to alvits and Gordon:

cmd=(find /usr/cti/conf/backup -name "*.tgz*")
${cmd[@]}
Advertisement