≡ Menu

shell variable

HowTo: Get Current Time In Shell Script

How do I get the current server time in shell script on Linux or Unix-like operating systems? How do I store the current time in the shell variable and use in my scripts?
[click to continue…]

FreeBSD csh / tcsh: Export Shell Variable

Q. I'm using FreeBSD 7 with csh (tcsh) shell. How do I export shell variable under FreeBSD operating systems?
[click to continue…]

Bash Shell Loop Over Set of Files

How do I run shell loop over set of files stored in a current directory or specified directory?
[click to continue…]

Find the file permission without using ls -l command

Q. I need to find file permission and store the same to a shell variable. How do I find out the file permission without parsing ls -l output?

A. Use GNU stat command to display file or file system status. It has option to display output in specific format.

Display stat for /etc/passwd file

Type the following command:
$ stat /etc/passwd

  File: `/etc/passwd'
  Size: 1675            Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 7899368     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2007-12-05 08:20:31.000000000 +0530
Modify: 2007-12-04 23:36:50.000000000 +0530
Change: 2007-12-04 23:36:50.000000000 +0530

Print access rights in octal format:
$ stat -c %a /etc/passwd


Print access rights in human readable format, enter:
$ stat -c %A /etc/passwd


Store access rights in octal format to a shell variable:
$ VAR=$(stat -c %a /etc/passwd)
$ echo $VAR

Other valid format sequences for files

  • %b : Number of blocks allocated (see %B)
  • %B : The size in bytes of each block reported
  • %d : Device number in decimal
  • %C : SELinux security context
  • %D : Device number in hex
  • %f : Raw mode in hex
  • %F : File type
  • %g : Group ID of owner
  • %G : Group name of owner
  • %h : Number of hard links
  • %i : Inode number
  • %n : File name
  • %N : Quoted file name with dereference if symbolic link
  • %o : I/O block size
  • %s : Total size, in bytes
  • %t : Major device type in hex
  • %T : Minor device type in hex
  • %u : User ID of owner
  • %U : User name of owner
  • %x : Time of last access
  • %X : Time of last access as seconds since Epoch
  • %y : Time of last modification
  • %Y : Time of last modification as seconds since Epoch
  • %z : Time of last change
  • %Z : Time of last change as seconds since Epoch

How to: create a temporary file securely

Q. How do I create a temporary file securely under UNIX Bash shell?

A. There are many ways:
[a] mktemp command - make temporary unique filename

[b] $RANDOM - Use $RANDOM variable

mktemp command

From the man page:

The mktemp utility takes the given filename template and overwrites a portion of it to create a unique filename. The template may be any filename with some number of ‘Xs’ appended to it, for example /tmp/tfile.XXXXXXXXXX.

For example, create a temporary file:
$ mktemp /tmp/output.XXXXXXXXXX


Send ls command output to /tmp/output.qBYDtF7199:
$ ls > /tmp/output.qBYDtF7199
However, you may need to store temporary file name /tmp/output.qBYDtF7199 to a shell variable:
TMPFILE=$(mktemp /tmp/output.XXXXXXXXXX)

Create a temporary directory

The -d option makes a directory instead of a file.
TMPDIR=$(mktemp -d /tmp/output.XXXXXXXXXX)
# do something

Another example:

TMPFILE=‘mktemp -t /tmp/out.myapp.XXXXXXXXXX‘ && {
    # Safe to use $TMPFILE in this block
    echo data > $TMPFILE
   # do something
   # clean up
    rm -f $TMPFILE

Using $RANDOM variable

Bash also provide $RANDOM variable with random value, you can use the same to create a file or directory:

# create directory
mkdir $TEMDIR
# do something...

Force Linux / UNIX bash shell to correct spelling of a directory name

Q. There is a way to cd a particular directory even if you spelled incorrectly on the command line. The command was added to my shell startup file. After formatting my hard disk, I lost my original file. Can you tell me the shell option or command that will automatically correct errors in the spelling of a directory name in a cd command?

A. There is a bash shell variable called cdspell If set, minor errors in the spelling of a directory component in a cd command will be corrected. The errors checked for are transposed characters, a missing character, and one character too many. If a correction is found, the corrected file name is printed, and the command proceeds. This option
is only used by interactive shells.

To toggle the values of cdspell variables controlling optional behavior use shopt command as follows:
$ shopt -s cdspell
$ cd /etx
$ pwd

Add above command to your .bash_profile file.

BASH Shell: Change The Color of My Shell Prompt Under Linux or UNIX

How do I change the color of my shell prompt under Linux or Unix operating systems?
[click to continue…]