Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a string which comes from a variable I want to increment it. how can i do that using shell script?

this is my input which comes from a variable:

abc-ananya-01

output should be:

abc-ananya-02
See Question&Answers more detail:os

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

1 Answer

It is shorter:

a=abc-lhg-08
echo ${a%-*}-`printf "%02d" $((10#${a##*-}+1))`
abc-lhg-09

Even better:

a=abc-lhg-08
printf "%s-%02d
" ${a%-*} $((10#${a##*-}+1))
abc-lhg-09

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...