LinuxTutorials

Bash string manipulation in program arguments

In this code snippet, I would like to run an application with a URL payload based on date and time. This
code will be executed in a specific schedule everyday and I would like to dynamically inject the date and time in
the program argument when the program executes.


./json2csv rules.json "ncp" "http://localhost:8080/api/zget?eid=get-ncp-mv-by-starttime-endtime&starttime=$(date --date='yesterday' +\%Y-\%m-\%d)+00:00:00&endtime=$(date --date='yesterday' +\%Y-\%m-\%d)+23:59:00&container=false" > $(echo "nercc_mv_$(date --date='yesterday' +\%Y-\%m-\%d).csv")

The way to do this is using the Bash String Manipulation

Inside a string, the bash will evaluate statement inside “$()” in this example:

$(date --date='yesterday' +\%Y-\%m-\%d)


"http://localhost:8080/api/zget?eid=get-ncp-mv-by-starttime-endtime&starttime=$(date --date='yesterday' +\%Y-\%m-\%d)+00:00:00&endtime=$(date --date='yesterday' +\%Y-\%m-\%d)+23:59:00&container=false"

The resulting string after bash string manipulation will be like this

http://localhost:8080/api/zget?eid=get-ncp-mv-by-starttime-endtime&starttime=2018-09-27+00:00:00&endtime=2018-09-27+23:59:00&container=false

Leave a Reply

Your email address will not be published. Required fields are marked *