var1="Oct 10 07:34"

I want the output as Oct 10 07:35 (value 35 needs to be from the var2).


Use shell (POSIX sh/bash/Korn/zsh) parameter substitution expansion. as following.

echo "${var1%:*}:${var2}"
  • ${var1%:*} (cut-up-to-first-suffix): This removes everything from end of string until first colon : seen, then print a colon :, finally print value of variable var2
|improve this answer|||||

With bash or recent versions of zsh:

$ var1="Oct 10 07:34"
$ var2=35
$ echo "${var1:0:-2}${var2}"


Oct 10 07:35

With zsh or yash:

$ echo "${var1[1,-3]}$var2"
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.