Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

eval - Bash function calling command given as argument

How do you write a function in bash that executes the command that it is given as an argument, where

  • The given command may be an alias
  • Arguments must be passed on exactly as given; no evaluating may be done

In other words, how to write an as-transparent-as-possible wrapper function.

The goal of the wrapper function could for example be to set the current directory before and after the given command, and/or set environment variables, or time how long the given command takes,... As a simple example here I take a function that just prints a line and then executes the given command.

A first attempt:

function wrap1 {
   echo Starting: "$@"
   "$@"
}

You could use it like wrap1 echo hello. But the problem is you cannot do alias myalias echo and then call wrap1 myalias hello: it wouldn't resolve the alias.

Another attempt using eval:

function wrap2 {
   echo Starting: "$@"
   eval "$@"
}

Now calling an alias works. But the problem is it evaluates the arguments too. For example wrap2 echo "\a" prints just a instead of a because the arguments are evaluated twice.

shopt -s expand_aliases doesn't seem to help here either.

Is there a way to both evaluate aliases like wrap2, but still pass on the arguments directly like wrap1?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You (uh, I) can use printf %q to escape the arguments. At first sight, escaping with printf and then doing eval always gives the same result as passing the arguments directly.

wrap() {
    echo Starting: "$@"
    eval $(printf "%q " "$@")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...