Here are 12 useful tricks for more effective bash shell use and programming:
Hack #1: Generating a random number:
The environment variable $RANDOM produces a random integer between 0 and (2^16)-1. You can use it in a shell script to randomly select a list of instructions to execute.
Code:arr=([0]="Random string #1" [1]="Random string #2" [2]="Random string #3"); declare -i randomString=$RANDOM%3; echo $randomString
Hack #2: Toggling between directories:
Use cd - to toggle between the current directory and the last one. The directory you switch to is printed to the terminal.
Hack #3: pushd, popd, and dirs:
Unix has a useful object called the directory stack, which provides a path to take through your filesystem.
pushd directory adds directory to the top of the directory stack. When you pop the stack, bash brings you to the directory at the top of the stack.
popd pops the directory stack, then displays the current directory plus the directory stack.
dirs displays the directory stack as if you had just popped it.
Hack #4: Using aliases:
An alias is a shortcut, an abbreviated command. Aliases are very useful for executing repetitive commands very quickly. It is often useful to store aliases in a script, like this:
Typing the name of an alias executes whatever command its value is set to.Code:#!/bin/bash # This script creates aliases for annoyingly long and tedious cd commands. alias gotoscripts="cd ~/Desktop/SourceCodes/Scripts"; alias gotoworkfiles="cd ~/Desktop/WorkFiles/Current"; alias gotoruby="cd ~/Desktop/SourceCodes/Ruby";
You can delete an alias by typing unalias "myalias".
Hack #5: CDPATH:
$CDPATH is an environment variable that sets the default file path for directory changes. If you enter cd and then the name of a directory in $CDPATH, bash automatically brings you to that directory. Here's the syntax:
If you have a directory named bin in usr/local, typing cd bin will always bring you to /usr/local/bin, even if you are outside the containing directory.Code:export CDPATH=/usr/local
Hack #6: sort
The command outputs the lines of a file sorted alphabetically. To sort a file, type sort filename at the prompt. You can then use I/O redirection to send the sorted output to another file.
Hack #7: cal
cal is a Unix program that prints a calendar of dates. The default is to display the current month, like this:
There are several options for displaying the calendar in different formats. Here are two of them:Code:bash-2.3$ cal December 2009 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
cal -m Jul - display the calendar for July of this year.
cal -y 2007 - display the calendar for all months in 2007.
Hack #8: Path to a command:
The type program shows the path to the binary for a command if it is a regular command, and prints "---- is a shell builtin" if the command is a builtin. This is useful for determining what to put in the she-bang line of a script:
The she-bang for perl scripts is then #!/usr/bin/perl.Code:bash-3.2$ type perl /usr/bin/perl
Hack #9: Formatting the date:
There are several specifiers for formatting the date. These are used by typing a + and then a string in quotes with the specifiers in it. The shell will replace the format specifiers with the parts of the date.
Example:
Options:Code:bash-3.2$ date +"%m/%d/%y" 12/02/09
%m - month
%d - day
%y - abbreviated year
%D - month-day-year
%Y - full year
%a - abbrevated day name
%A - full day name
%b - abbrevated month name
%B - full month name
Hack #10: Word count:
wc counts the words, characters, or bytes in the given file. It's options are:
-c - number of bytes
-l - number of lines
-m - number of characters (including multibyte characters)
-w - number of words
Hack #11: Command history:
There are three useful hacks for using the command history of the terminal:
history - this command prints the command history.
HISTTIMEFORMAT is an environment variable that specifies the format for displaying the command history. There are a few formatting options for this variable.
This displays the date and time for each command in the history.Code:export HISTTIMEFORMAT "%F %T"
The command history can be searched by typing Ctrl+R and then typing a keyword.
Hack #12: Listing open files:
lsof lists all open files with information about each of them. This information includes process name, process ID, user, file descriptor, type (DIR or REG), device number, size, node number, and name of the file.
Summary of environment variables:
$CDPATH - the default file path
$COLUMNS - the width of the terminal window, in characters
$HISTTIMEFORMAT - the format for displaying the command history
$LINES - the height of the terminal window, in characters
$OLDPWD - previous directory
$PATH - a list of paths to the currently running program
$PWD - current directory
$RANDOM - random number
$SHELL - the path to the shell
$TERM - the name of the terminal
Life's too short to be cool. Be a nerd.
I didn't know about some of these things.
![]()
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Good easy to follow tutorial on bash, thanks
+rep
Things I did not know, nice, more rep (the little I have).
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks