bash - Picking values from a variable -


i have variable in shell script looks this:

dir="home_x_y_z"; 

it represents directory name. imagine have multiple directories named this:

home_1_2_z/ home_9_a_z/ home_3_5_z/ etc... 

the values change located @ position x , y. there regular expression use (maybe sed or awk) peel values out? want able used them independently elsewhere in script.

so able have 2 more variables:

x_value=""; y_value="";  

thanks

this easy pure bash:

ifs='_' read x_value y_value z_value <<< "${dir#*_}" 

explanation:
so, ${dir#*_} going remove shortest match beggining of string. see parameter expansion cheat sheet more stuff that.

echo ${dir#*_} # returns x_y_z 

after have change default ifs _ , read our values.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -