shell - Unix : saving a sed command in a variable, to echo it, then execute it -


im new unix scripts, sorry mistake may :)

i execute "sed" command.
first echo command executed.
so, save command in variable.
then, execute command.

what did (simplyfied version of .sh script) :

command="sed -i 's/foo/bar/g' myfile"; echo "command: \"$command\"" ; $command ; 

i got error :

command: "sed -i 's/foo/bar/g' myfile" sed: -e expression #1, char 1: unknown command: `'' 

how should escape single quotes ? (or may use double quotes ?)

(what found on google on error did not help)

define convenience function echo given command, run it.

verbosely_do () {   printf 'command: %s\n' "$*";  # printf, not echo, because $@ might contain switches echo   "$@"; }  verbosely_do sed -i 's/foo/bar/g' myfile 

this give you:

command: sed -i s/foo/bar/g myfile 

and perform sed(1) command.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -