andrefurquin
andrefurquin

Reputation: 522

Change character of a string in shell script

What is the best way to change a character in a string in shell script ?

I have the following variable:

A="2017-03-16 18:00:00"

I would like to change it for "2017-03-16 18:00:01" (adding +1)

Upvotes: 0

Views: 70

Answers (2)

SLePort
SLePort

Reputation: 15461

If you want to Change character of a string in shell script as mentionned in your title, you can use parameter expansion:

$ A="2017-03-16 18:00:00"
$ echo ${A%0}1
2017-03-16 18:00:01

If you want to add 1 second to a date, then Hunter McMillen's answer does the job.

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61515

You can use the date command to add 1 second to a specific date:

$ date -d "2017-03-16 18:00:00+1 seconds"
Thu Mar 16 13:00:01 EDT 2017

(Note that this converted to local timezone EST)

Incorporated into your script, that would be:

#!/bin/sh 

A="2017-03-16 18:00:00"
date -d "$A+1 seconds"

Upvotes: 2

Related Questions