复制一部分fullpath变量并使用该部分在shell中构造另一个不同的fullpath变量

时间:2012-06-28 11:32:10

标签: regex shell

我有一个变量var1 ='common_location / specific_location_1 / common_filename_text'现在我必须得到一个新变量var2 ='common_location / specific_location_2 / common_filename_text'。 我怎么能在linux shell中做到这一点。我是shell脚本的新手。所以请假设我知道的很少。

1 个答案:

答案 0 :(得分:0)

使用:

var2=${var1/1/2}

其中var1变量,其中存储了/specific_location_1/common_filename_text。 您可以在下方看到var1var2仅与一个符号

不同
var1='common_location/specific_location_1/common_filename_text'
var2='common_location/specific_location_2/common_filename_text'

所以我们只需要替换1到2并获得你需要的新变量。 你可以用:

var2=${var1/1/2}

非常简单。