Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It offers functional improvements over sh for both programming and interactive use; these include command line editing, unlimited size command history, job control, shell functions and aliases, indexed arrays of unlimited size, and integer arithmetic in any base from two to sixty-four. Bash can run most sh scripts without modification. Bash Tricks are widely used for both programmers and system administrators. But this article focus about the tricks for bash programmers
Bash has several commands that comes with the shell (i.e built inside the bash shell). When you execute a built-in command, bash shell executes it immediately, without invoking any other program. Bash shell built-in commands are faster than external commands, because external commands usually fork a process to execute it.
Join Command
Join command combines lines from two files based on a common field.
In the example below, we have two files – employee.txt and salary.txt. Both
have employee-id as common field. So, we can use join command to combine
the data from these two files using employee-id as shown below.
# cat name.txt
1 bob
2 john
3 sunil
4 jane
5 alice
# cat percentage.txt
1 80%
2 90%
3 69%
4 56%
5 89%
# join name.txt percentage.txt
1 bob 80%
2 john 90%
3 sunil 69%
4 jane 56%
5 alice 89%
Export Command Example
export command is used to export a variable or function to the environment of all the child processes running in the current shell.
#export variablename=value
#export -f functionname # exports a function in the current shell.
It exports a variable or function with a value. “env” command lists all the environment variables. In the following example, you can see that env displays the exported variable.
# export ipsr=redhat
# env
GDM_XSERVER_LOCATION=local
PWD=/root
INPUTRC=/etc/inputrc
XMODIFIERS=@im=none
ipsr=redhat
LANG=en_US.UTF-8
KDE_IS_PRELINKED=1
GDMSESSION=gnome
# echo $ipsr
redhat
“export -p” command also displays all the exported variable in the current shell.
hash Command Example
hash command maintains a hash table, which has the used command’s path names. When you execute a command, it searches for a command in the variable $PATH.
But if the command is available in the hash table, it picks up from there and executes it. Hash table maintains the number of hits encountered for each commands used so far in that shell.
# hash
hits command
1 /bin/egrep
18 /usr/bin/ac
3 /usr/bin/man
1 /usr/bin/clear
You can delete a particular command from a hash table using -d option, and -r option to reset the complete hash table.
#hash -d egrep
# hash
hits command
18 /usr/bin/ac
3 /usr/bin/man
1 /usr/bin/clear
readonly Command Example
readonly command is used to mark a variable or function as read-only, which can not be changed further.
# readonly computer=IT
#echo $computer
IT
#read computer
CSE
bash: computer: readonly variable
test Command Example
test command evaluates the conditional expression and returns zero or one based on the evaluation. Refer the manual page of bash, for more test operators.
#! /bin/bash
if test -z $1
then
echo "The positional parameter \$1 is empty"
fi
Note: Here ‘z’ option checks the length of STRING is zero
set Command Examples
set is a shell built-in command, which is used to set and modify the internal variables of the shell. set command without argument lists all the variables and it’s values. set command is also used to set the values for the positional parameters.
$ set +o history # To disable the history storing.
+o disables the given options.
$ set -o history
-o enables the history
$ cat set.sh
var="Welcome to ipsr"
set -- $var
echo "\$1=" $1
echo "\$2=" $2
echo "\$3=" $3
$ ./set.sh
$1=Welcome
$2=to
$3=ipsr
Note: The args follwed by “–-” option is used to set the values to the positional parameter. If no arguments follow this option, then the positional parameters are unset.
unset Command Examples
unset built-in is used to set the shell variable to null. unset also used to delete an element of an array and to delete complete array.
$ cat unset.sh
#!/bin/bash
#Assign values and print it
var="welcome to ipsr"
echo $var
#unset the variable
unset var
echo $var
$ ./unset.sh
welcome to ipsr
In the above example, after unset the variable “var” will be assigned with null string.
let Command Example
let commands is used to perform arithmetic operations on shell variables.
$ cat arith.sh
#! /bin/bash
let arg1=12
let arg2=11
let add=$arg1+$arg2
let sub=$arg1-$arg2
let mul=$arg1*$arg2
let div=$arg1/$arg2
echo $add $sub $mul $div
$ ./arith.sh
23 1 132 1
printf Command Example
Similar to printf in C language, bash printf built-in is used to format print operations.
In the above example, the script does arithmetic operation on two inputs. In that script instead of echo statement, you can use printf statement to print formatted output as shown below.
In arith.sh, replace the echo statement with this printf statement.
printf "Addition=%d\nSubtraction=%d\nMultiplication=%d\nDivision=%f\n" $add $sub $mul $div
$ ./arith.sh
Addition=23
Subtraction=1
Multiplication=132
Division=1.000000
Display total connect time of users
Ac command will display the statistics about the user’s connect time.
Connect time for the current logged in user
With the option –d, it will break down the output for the individual days. In
this example, I’ve been logged in to the system for more than 6 hours today.
On Dec 1st, I was logged in for about 1 hour.
$ ac –d
Dec 1 total 1.08
Dec 2 total 0.99
Dec 3 total 3.39
Dec 4 total 4.50
Today total 6.10
To display connect time for all the users use –p as shown below. Please note
that this indicates the cumulative connect time for the individual users.
$ ac -p
user1 3.64
root 229.12
user2 88.17
john 105.92
jane 111.42
total 538.27
To get a connect time report for a specific user, execute the following:
$ ac -d root
Aug 13 total 13.30
Aug 14 total 13.49
Aug 16 total 17.67
Aug 17 total 20.20
Aug 18 total 15.41
Aug 19 total 58.67
Aug 26 total 31.13
Aug 28 total 51.24
Today total 8.35
posted by:
IPSR linuxgroup