time=$(date “+%Y%m%d-%H%M%S”)
or
time=$(date “+%Y-%m-%d %H:%M:%S”)
…… etc. in any format you want
echo “${time}”
The above two lines of simple code is the shell to get the current time and output it in the format you want.
A few things to note
There is a space after date, otherwise the command cannot be recognized, the shell is still very strict about spaces.
Y shows 4-digit year, e.g. 2018; y shows 2-digit year, e.g. 18. m shows month; M shows minute. d shows day, while D shows current date, e.g. 1/18/18 (i.e. 2018.1.18). h shows hour, while h shows month (a bit confusing). s shows current second in milliseconds; S shows current second in seconds.
Read More:
- Prompt “entering emergency mode. Exit the shell to continue” if the Linux operating system does not start normally
- How to Use Apt get Command Under Mac OSX
- Windows writes shell script and executes shell error [Solved]
- C#: How to get the value or text value of the select drop-down list
- How to Solve Xmind Install Error in Ubuntu System
- The JSON variable is parsed in the shell to obtain the value corresponding to the key
- [Solved] docker Error: System has not been booted with systemd as init system (PID 1). Can‘t operate. Failed to con
- Centos7 Start Error: Entering emergency mode.Exit the shell to continue
- [Solved] chroot: failed to run command ‘mount’: Exec format error
- print(size_t) Format (You Need to Know)
- System has not been booted with systemd as init system (PID 1). Can‘t operate
- Solution to gzip: stdin: invalid compressed data — format violated error in decompressing. Tgz file under Linux
- [Solved] Ubuntu 20.04.3 Fail to Start sshd Error: Failed to start OpenBSD Secure Shell server
- Shell Script syntax error near unexpected token `done’
- Ubuntu shell Script syntax error near unexpected token `$‘{\r‘‘
- Using apt get to delete software completely
- Running shell script reports an error: “syntax error near unexpected token solution ‘”
- OSS Error: The difference between the request time and the current time is too large
- [Solved] Shell error: syntax error: unexpected end of file
- Linux system service command error: Failed to allocate directory watch: Too many open files