10.10.2014

HowTo Format Date For Display or Use In a Shell Script

You need to use the standard date command to format date or time. You can use the same command with the shell script.
Syntax
The syntax is
date +FORMAT
OR
date +"%FORMAT"
OR
date +"%FORMAT%FORMAT"
OR
date +"%FORMAT-%FORMAT"
Task: Display date in mm-dd-yy format
Open a terminal and type the following date command:
$ date +"%m-%d-%y"
Sample output:
02-27-07
To turn on 4 digit year display:
$ date +"%m-%d-%Y"
Just display date as mm/dd/yy format:
$ date +"%D"
Task: Display time only
Type the following command:
$ date +"%T"
Outputs:
19:55:04
To display locale's 12-hour clock time, enter:
$ date +"%r"
Outputs:
07:56:05 PM
To display time in HH:MM format, type:
$ date +"%H-%M"
Sample outputs:
00-50
How do I save time/date format to the shell variable?
Simply type the following command at the shell prompt:
$ NOW=$(date +"%m-%d-%Y")
To display a variable use
 echo / printf command:
$ echo $NOW
A sample shell script
#!/bin/bash
NOW=$(date +"%m-%d-%Y")
FILE="backup.$NOW.tar.gz"
echo "Backing up data to /nas42/backup.$NOW.tar.gz file, please wait..."
# rest of script
# tar xcvf /nas42/backup.$NOW.tar.gz /home/ /etc/ /var

A complete list of FORMAT control characters supported by the date command
FORMAT controls the output. It can be the combination of any one of the following:
%FORMAT String
Description
%%
a literal %
%a
locale's abbreviated weekday name (e.g., Sun)
%A
locale's full weekday name (e.g., Sunday)
%b
locale's abbreviated month name (e.g., Jan)
%B
locale's full month name (e.g., January)
%c
locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C
century; like %Y, except omit last two digits (e.g., 21)
%d
day of month (e.g, 01)
%D
date; same as %m/%d/%y
%e
day of month, space padded; same as %_d
%F
full date; same as %Y-%m-%d
%g
last two digits of year of ISO week number (see %G)
%G
year of ISO week number (see %V); normally useful only with %V
%h
same as %b
%H
hour (00..23)
%I
hour (01..12)
%j
day of year (001..366)
%k
hour ( 0..23)
%l
hour ( 1..12)
%m
month (01..12)
%M
minute (00..59)
%n
a newline
%N
nanoseconds (000000000..999999999)
%p
locale's equivalent of either AM or PM; blank if not known
%P
like %p, but lower case
%r
locale's 12-hour clock time (e.g., 11:11:04 PM)
%R
24-hour hour and minute; same as %H:%M
%s
seconds since 1970-01-01 00:00:00 UTC
%S
second (00..60)
%t
a tab
%T
time; same as %H:%M:%S
%u
day of week (1..7); 1 is Monday
%U
week number of year, with Sunday as first day of week (00..53)
%V
ISO week number, with Monday as first day of week (01..53)
%w
day of week (0..6); 0 is Sunday
%W
week number of year, with Monday as first day of week (00..53)
%x
locale's date representation (e.g., 12/31/99)
%X
locale's time representation (e.g., 23:13:48)
%y
last two digits of year (00..99)
%Y
year
%z
+hhmm numeric timezone (e.g., -0400)
%:z
+hh:mm numeric timezone (e.g., -04:00)
%::z
+hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z
numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z
alphabetic time zone abbreviation (e.g., EDT)


What is a Unix Shell?

The shell provides you with an interface to the UNIX system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output.
A shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of shells, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

Shell Prompt:

The prompt, $, which is called command prompt, is issued by the shell. While the prompt is displayed, you can type a command.
The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.
Following is a simple example of date command which displays current date and time:
$date
Thu Jun 25 08:30:19 MST 2009
You can customize your command prompt using environment variable PS1 explained in Environment tutorial.

Shell Types:

In UNIX there are two major types of shells:
  1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $ character.
  2. The C shell. If you are using a C-type shell, the default prompt is the % character.
There are again various subcategories for Bourne Shell which are listed as follows:
  • Bourne shell ( sh)
  • Korn shell ( ksh)
  • Bourne Again shell ( bash)
  • POSIX shell ( sh)
The different C-type shells follow:
  • C shell ( csh)
  • TENEX/TOPS C shell ( tcsh)
The original UNIX shell was written in the mid-1970s by Stephen R. Bourne while he was at AT&T Bell Labs in New Jersey.
The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as "the shell".
The Bourne shell is usually installed as /bin/sh on most versions of UNIX. For this reason, it is the shell of choice for writing scripts to use on several different versions of UNIX. 

10.09.2014

Bash (Unix shell)

Bash is a Unix shell written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell (sh).Released in 1989,  it has been distributed widely as the shell for the GNU operating system and as a default shell on Linux and Mac OS X. It has been ported to Microsoft Windows and distributed with Cygwin and MinGW, to DOS by the DJGPP project, to Novell NetWare and to Android via various terminal emulation applications. In the late 1990s, Bash was a minor player among multiple commonly used shells, unlike presently where Bash has overwhelming favor.

Bash is a command processor that typically runs in a text window, where the user types commands that cause actions. Bash can also read commands from a file, called a script. Like all Unix shells, it supports filename wildcarding, piping, here documents, command substitution, variables and control structures for condition-testing and iteration. The keywords, syntax and other basic features of the language were all copied from sh. Other features, e.g., history, were copied from csh and ksh. Bash is a POSIX shell, but with a number of extensions.

The name itself is an acronym, a pun, and a description. As an acronym, it stands for Bourne-again shell, referring to its objective as a free replacement for the Bourne shell. As a pun, it expressed that objective in a phrase that sounds similar to born again, a term for spiritual rebirth. The name is also descriptive of what it did, bashing together the features of sh, csh, and ksh

Keyboard shortcuts

The following shortcuts work when using default (Emacs) key bindings. Vi-bindings can be enabled by running set -o vi.


Note: For shortcuts involving Alt, you may be able to use Esc instead.


Note: Sometimes, you must use Esc instead of Alt, because the Alt shortcut conflicts with another shortcut. For example, in Trisquel 5.0 (a distribution of Linux), pressing Alt+f does not move the cursor forward one word, but activates "File" in the menu of the terminal window.


See also: Readline

Tab ↹ : Autocompletes from the cursor position.

Ctrl+a : Moves the cursor to the line start (equivalent to the key Home).

Ctrl+b : Moves the cursor back one character (equivalent to the key ←).

Ctrl+c : Sends the signal SIGINT to the current task, which aborts and closes it.

Ctrl+d

Sends an EOF marker, which (unless disabled by an option) closes the current shell (equivalent to the command exit). (Only if there is no text on the current line)

If there is text on the current line, deletes the current character (then equivalent to the key Delete).

Ctrl+e : (end) moves the cursor to the line end (equivalent to the key End).

Ctrl+f : Moves the cursor forward one character (equivalent to the key →).

Ctrl+g : Abort the research and restore the original line.

Ctrl+h : Deletes the previous character (same as backspace).

Ctrl+i : Equivalent to the tab key.

Ctrl+j : Equivalent to the enter key.

Ctrl+k : Clears the line content after the cursor and copies it into the clipboard.

Ctrl+l : Clears the screen content (equivalent to the command clear).

Ctrl+n : (next) recalls the next command (equivalent to the key ↓).

Ctrl+o : Executes the found command from history, and fetch the next line relative to the current line from the history for editing.

Ctrl+p : (previous) recalls the prior command (equivalent to the key ↑).

Ctrl+r : (research) recalls the last command including the specified character(s). A second Ctrl+r recalls the next anterior command that corresponds to the research

Ctrl+s : Go back to the next more recent command of the research (beware to not execute it from a terminal because this command also launches its XOFF). If you changed that XOFF setting, use Ctrl+q to return.

Ctrl+t : Transpose the previous two characters.

Ctrl+u : Clears the line content before the cursor and copies it into the clipboard.

Ctrl+v : If the next input is also a control sequence, type it literally (e. g. * Ctrl+v Ctrl+h types "^H", a literal backspace.)

Ctrl+w : Clears the word before the cursor and copies it into the clipboard.

Ctrl+x Ctrl+e : Edits the current line in the $EDITOR program, or vi if undefined.

Ctrl+x Ctrl+r : Read in the contents of the inputrc file, and incorporate any bindings or variable assignments found there.

Ctrl+x Ctrl+u : Incremental undo, separately remembered for each line.

Ctrl+x Ctrl+v : Display version information about the current instance of Bash.

Ctrl+x Ctrl+x : Alternates the cursor with its old position. (C-x, because x has a crossing shape).

Ctrl+y : (yank) adds the clipboard content from the cursor position.

Ctrl+z : Sends the signal SIGTSTP to the current task, which suspends it. To execute it in background one can enter bg. To bring it back from background or suspension fg ['process name or job id'] (foreground) can be issued.

Ctrl+_ : Incremental undo, separately remembered for each line.

Alt+b : (backward) moves the cursor backward one word.

Alt+c : Capitalizes the character under the cursor and moves to the end of the word.

Alt+d : Cuts the word after the cursor.

Alt+f : (forward) moves the cursor forward one word.

Alt+l : Lowers the case of every character from the cursor's position to the end of the current word.

Alt+r : Cancels the changes and puts back the line as it was in the history.

Alt+u : Capitalizes every character from the cursor's position to the end of the current word.

Alt+. : Insert the last argument to the previous command (the last word of the previous history entry).a

240 LINUX U HAVE TO TRY

 How many have you tried? Have a look

Linux is all about freedom, experiments and versatility! We all know about popular Linux distros like Linux Mint, Ubuntu, Fedora, Redhat etc., but do you know that there are over 400 Linux distros! So if the adventurous geek inside you is still alive.

 
Absolute LinuxOfficial website
AbulEduOfficial website
AgiliaLinuxOfficial website
aLinuxOfficial website
Alpine LinuxOfficial website
ALT LinuxOfficial website
AntergosOfficial website
antiXOfficial website
aptosidOfficial website
Arch LinuxOfficial website
ArchBang LinuxOfficial website
ArchieOfficial website
AriOSOfficial website
ArtistXOfficial website
AsteriskNOWOfficial website
AsturixOfficial website
AUSTRUMIOfficial website
AV LinuxOfficial website
BackBox LinuxOfficial website
Berry LinuxOfficial website
BigLinuxOfficial website
Bio-LinuxOfficial website
blackPantherOfficial website
BlankOn LinuxOfficial website
Bluewall GNULinuxOfficial website
Bluewhite64 LinuxOfficial website
Bodhi LinuxOfficial website
BOSS LinuxOfficial website
Bridge LinuxOfficial website
CAELinuxOfficial website
CAINEOfficial website
Calculate LinuxOfficial website
CanaimaOfficial website
CàtixOfficial website
CDlinuxOfficial website
CentOSOfficial website
Chakra GNU/LinuxOfficial website
ClearOSOfficial website
ClonezillaOfficial website
ComFusionOfficial website
CrunchBang LinuxOfficial website
CRUXOfficial website
Damn Small LinuxOfficial website
DebianOfficial website
DEFT LinuxOfficial website
Descent|OSOfficial website
Devil-LinuxOfficial website
DigAnTelOfficial website
DoudouLinuxOfficial website
DragonFly BSDOfficial website
Dragora GNU/LinuxOfficial website
DreamStudio UnityOfficial website
Easy PeaseOfficial website
EdubuntuOfficial website
EkaatyOfficial website
ElastixOfficial website
EliveOfficial website
EmmabuntüsOfficial website
Endian FirewallOfficial website
Epidemic GNU/LinuxOfficial website
ExTiXOfficial website
FedoraOfficial website
FinnixOfficial website
Foresight LinuxOfficial website
FreeBSDOfficial website
FreeNASOfficial website
FrenzyOfficial website
Frugalware LinuxOfficial website
Fusion LinuxOfficial website
GALPon MiniNoOfficial website
GeeXboXOfficial website
GentooOfficial website
GhostBSDOfficial website
gNewSenseOfficial website
GPartedOfficial website
Greenie LinuxOfficial website
GrmlOfficial website
GuadalinexOfficial website
Hacao LinuxOfficial website
Hanthana LinuxOfficial website
Hybryde LinuxOfficial website
IPCop FirewallOfficial website
IPFireOfficial website
ISlackOfficial website
JibbedOfficial website
JolicloudOfficial website
kademarOfficial website
Kahel OSOfficial website
Kali LinuxOfficial website
KANOTIXOfficial website
KaroshiOfficial website
Kiwi linuxOfficial website
KnoppixOfficial website
Kubuntu LinuxOfficial website
KwheezyOfficial website
Kwort LinuxOfficial website
Leeenux LinuxOfficial website
Legacy OSOfficial website
Liberté LinuxOfficial website
Lightweight Portable SecurityOfficial website
LinExOfficial website
LinHESOfficial website
Linpus LinuxOfficial website
Linux DeepinOfficial website
Linux From ScratchOfficial website
Linux LiteOfficial website
Linux MintOfficial website
LinuxConsoleOfficial website
LliureXOfficial website
LubuntuOfficial website
Lunar-LinuxOfficial website
LuninuX OSOfficial website
LXLEOfficial website
m0n0wallOfficial website
MacpupOfficial website
MageiaOfficial website
MakuluLinuxOfficial website
MandrivaOfficial website
Manjaro LinuxOfficial website
MidnightBSDOfficial website
MINIXOfficial website
Momonga LinuxOfficial website
MusixOfficial website
MythbuntuOfficial website
NetBSDOfficial website
NetrunnerOfficial website
NetSecL OSOfficial website
Network Security ToolkitOfficial website
NexentaStorOfficial website
NuTyXOfficial website
OjubaOfficial website
OLPC OSOfficial website
OpenBSDOfficial website
OpenELECOfficial website
OpenIndianaOfficial website
openmamba GNU/LinuxOfficial website
OpenMediaVaultOfficial website
openSUSEOfficial website
Openwall GNU*LinuxOfficial website
OphcrackOfficial website
Oracle LinuxOfficial website
Oracle SolarisOfficial website
OS4 OpenLinuxOfficial website
OverclockixOfficial website
Parabola GNU/LinuxOfficial website
PardusOfficial website
ParsixOfficial website
Parted MagicOfficial website
PC-BSDOfficial website
PCLinuxOSOfficial website
Pear OSOfficial website
PentooOfficial website
Peppermint OSOfficial website
pfSenseOfficial website
PHLAKOfficial website
Pinguy OSOfficial website
Plamo LinuxOfficial website
PLD Linux DistributionOfficial website
Plop LinuxOfficial website
Point LinuxOfficial website
PorteusOfficial website
ProxmoxOfficial website
Puppy LinuxOfficial website
PureOSOfficial website
Qomo LinuxOfficial website
Qubes OSOfficial website
QuirkyOfficial website
RaspbianOfficial website
Rebellin LinuxOfficial website
Red Flag LinuxOfficial website
Red Hat Enterprise LinuxOfficial website
Redo Backup & RecoveryOfficial website
Rocks ClusterOfficial website
ROSAOfficial website
Sabayon LinuxOfficial website
SalentOSOfficial website
Saline OSOfficial website
Salix OSOfficial website
SchilliXOfficial website
Scientific LinuxOfficial website
Semplice LinuxOfficial website
siductionOfficial website
Simplicity LinuxOfficial website
SkolelinuxOfficial website
SlackelOfficial website
SlackwareOfficial website
SLAXOfficial website
SliTaz GNU/LinuxOfficial website
SME ServerOfficial website
SmoothwallOfficial website
SnowlinuxOfficial website
SolydXKOfficial website
SophosOfficial website
Source Mage GNULinuxOfficial website
SparkyLinuxOfficial website
Springdale LinuxOfficial website
StellaOfficial website
Super OSOfficial website
Superb Mini ServerOfficial website
SuperXOfficial website
SUSEOfficial website
Swift LinuxOfficial website
SystemRescueCDOfficial website
T2 LinuxOfficial website
TailsOfficial website
ThinstationOfficial website
Tiny Core LinuxOfficial website
TooroxOfficial website
Toutou LinuxOfficial website
Trisquel GNU/LinuxOfficial website
TuquitoOfficial website
TurbolinuxOfficial website
TurnKey LinuxOfficial website
UberStudentOfficial website
UbuntuOfficial website
Ubuntu Christian EditionOfficial website
Ubuntu GNOMEOfficial website
Ubuntu Privacy RemixOfficial website
Ubuntu StudioOfficial website
UbuntuKylinOfficial website
Ultimate EditionOfficial website
Unity LinuxOfficial website
Univention Corporate ServerOfficial website
Untangle GatewayOfficial website
UtutoOfficial website
VectorLinuxOfficial website
VENENUX GNU/LinuxOfficial website
Vine LinuxOfficial website
VinuxOfficial website
VortexBoxOfficial website
Voyager LiveOfficial website
VyattaOfficial website
wattOSOfficial website
WebconvergerOfficial website
WifislaxOfficial website
Window Maker LiveOfficial website
XubuntuOfficial website
YellowDogOfficial website
ZevenOSOfficial website
Zorin OSOfficial website