Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 432191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:02:59+00:00 2026-05-12T20:02:59+00:00

shell>/bin/date -d 091029 20:18:02 +%s 1256827682 Similarly I created shell script: #My.sh myDate=`date +’%y%m%d_%H%M%S’`

  • 0
shell>/bin/date -d "091029 20:18:02" +%s
1256827682

Similarly I created shell script:

#My.sh
myDate=`date +'%y%m%d_%H%M%S'`
myDate1=`echo $myDate | sed 's/_/ /g'`
myDate2=`echo $myDate1 | sed 's/\([0-9][0-9][0-9][0-9][0-9][0-9]\) \([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\/bin\/date -d "\1 \2:\3:\4" +%s/'`
print $myDate2
`$myDate2`

But it doesn’t execute above command. WHy?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:02:59+00:00Added an answer on May 12, 2026 at 8:02 pm

    The notation:

    `$myDate2`
    

    expands $myDate2 and executes the command (and I’ll come back to why there are problems with that), and then captures the output – and tries to run the output.

    What you are looking for is eval:

    eval $myDate2
    

    Handling quotes is tricky – and eval is often a part of the answer. When you build up a string with internal quotes, you need to use eval to get the shell to put the quotes back together.

    One very useful tool that I have is a program called al – for argument list.

    #include <stdio.h>
    int main(int argc, char **argv)
    {
        while (*++argv != 0)
            puts(*argv);
        return(0);
    }
    

    It prints each separate argument on a separate line. It was almost the first thing I did when looking at what you are up to.

    myDate=`date +'%y%m%d_%H%M%S'`
    myDate1=`echo $myDate | sed 's/_/ /g'`
    myDate2=`echo $myDate1 | sed 's/\([0-9][0-9][0-9][0-9][0-9][0-9]\) \([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\/bin\/date -d "\1 \2:\3:\4" +%s/'`
    print $myDate2
    #`$myDate2`
    al $myDate2
    eval al $myDate2
    eval $myDate2
    

    The trace output from this was:

    + date +%y%m%d_%H%M%S
    + myDate=091029_082546
    + sed 's/_/ /g'
    + echo 091029_082546
    + myDate1='091029 082546'
    + sed 's/\([0-9][0-9][0-9][0-9][0-9][0-9]\) \([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\/bin\/date -d "\1 \2:\3:\4" +%s/'
    + echo 091029 082546
    + myDate2='/bin/date -d "091029 08:25:46" +%s'
    + print /bin/date -d '"091029' '08:25:46"' +%s
    /bin/date -d "091029 08:25:46" +%s
    + al /bin/date -d '"091029' '08:25:46"' +%s
    /bin/date
    -d
    "091029
    08:25:46"
    +%s
    + eval al /bin/date -d '"091029' '08:25:46"' +%s
    + al /bin/date -d '091029 08:25:46' +%s
    /bin/date
    -d
    091029 08:25:46
    +%s
    + eval /bin/date -d '"091029' '08:25:46"' +%s
    + /bin/date -d '091029 08:25:46' +%s
    usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
                [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
    

    Note how when I ran ‘al $myDate2’ the date string was split into two arguments, but when I ran ‘eval al $myDate2’, the string was one argument – as required. I was testing on MacOS X, where the data command does not accept the date string format you supplied – that is a whole separate problem. But getting the string healed requires ‘eval’.


    I didn’t even address the issue of what the shell script was trying to do.

    I gather from Hai Vu’s answer that we’re really after the current time in seconds since the epoch; I can sort of see how that might be.

    On MacOS X, that is obtained trivially:

    date +'%s'
    

    (where the single quotes really aren’t needed). The MacOS X manual page also includes the example:

          date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
    

    This seems a bit convoluted – but would allow you to find the seconds since the epoch for any date previously given by the date command – or a date that will be given at some time in the future (by replacing the back-quoted date with the previous string).

    An æon or so ago, I wrote programs ‘systime’ to print the current time as the number of seconds past the epoch, and also a program ‘timestamp’ to convert such values back into formatted dates – because none of the standard versions of the ‘date’ command supported such mechanisms back then (before the C standard was standard, and therefore before strftime() was widely available). I also have a program ‘strptime’ for converting a formatted date into a timestamp. Ah well – nice to know that the standard programs can now do it.

    However, I note that the MacOS ‘date’ command is a superset of the POSIX standard version; I suspect that the Linux (GNU) ‘date’ command is a different superset of the POSIX standard, and so on for each platform.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For testing purposes I have this shell script #!/bin/bash echo $$ find / >/dev/null
i am testing with the shell script below: #!/bin/ksh -x instance=`echo $1 | cut
I have some simple shell scripts #!/bin/bash echo $(date) NOW=$(date +%Y-%m-%d-%H-%M-%S) NAME = db.$NOW.sql
I've a cgi shell script like this #!/bin/sh # show the page echo Content-type:
Suppose a shell script (/bin/sh or /bin/bash) contained several commands. How can I cleanly
I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
Have a shell script that reads the files in a particular directory. #!/bin/bash for
I am writing a simple unix shell script: #!/bin/bash # abort the script if
I created a small shell script which logs all of it's input to a
I wrote the following shell script: #!/bin/bash MAIL=/usr/mail/${LOGNAME:?} . /installations/etl_scripts/etl_job.properties JOB_LOG_FILE_PATH=$JOB_LOG_FILE_PATH export JOB_LOG_FILE_PATH JOB_LOG_FILE_NAME=$JOB_LOG_FILE_NAME

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.