bash命令为变量 [英] Bash command as variable

查看:110
本文介绍了bash命令为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个sed命令开始存储这样的变量中:

I am trying to store the start of a sed command inside a variable like this:

sedcmd="sed -i '' "

后来我则像这样执行命令:

Later I then execute a command like so:

   $sedcmd s/$orig_pkg/$package_name/g $f

和它不工作。运行使用bash -x了剧本,我可以看到,它正在扩大,如:

And it doesn't work. Running the script with bash -x, I can see that it is being expanded like:

   sed -i ''\'''\''' 

什么是正确的方式来恩preSS呢?

What is the correct way to express this?

推荐答案

定义一个shell功能:

Define a shell function:

mysed () {
    sed -i "" "$@"
}

和调用它是这样的:

$ mysed s/$orig_pkg/$package_name/g $f

这篇关于bash命令为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆