Tutorial by Examples

VariableDetails$* / $@Function/script positional parameters (arguments). Expand as follows:$* and $@ are the same as $1 $2 ... (note that it generally makes no sense to leave those unquoted)"$*" is the same as "$1 $2 ..." 1 "$@" is the same as "$1" "$2&qu...
Process ID (pid) of the current instance of Bash. This is not the same as the $$ variable, but it often gives the same result. This is new in Bash 4 and doesn't work in Bash 3. ~> $ echo "\$\$ pid = $$ BASHPID = $BASHPID" $$ pid = 9265 BASHPID = 9265
An environment variable pointing to the Bash startup file which is read when a script is invoked.
An array containing the full version information split into elements, much more convenient than $BASH_VERSION if you're just looking for the major version: ~> $ for ((i=0; i<=5; i++)); do echo "BASH_VERSINFO[$i] = ${BASH_VERSINFO[$i]}"; done BASH_VERSINFO[0] = 3 BASH_VERSINFO[1] =...
Shows the version of bash that is running, this allows you to decide whether you can use any advanced features: ~> $ echo $BASH_VERSION 4.1.2(1)-release
The default editor that will be involked by any scripts or programs, usually vi or emacs. ~> $ echo $EDITOR vi
To get the name of the current function - type: my_function() { echo "This function is $FUNCNAME" # This will output "This function is my_function" } This instruction will return nothing if you type it outside the function: my_function echo "This function i...
The home directory of the user ~> $ echo $HOME /home/user
The hostname assigned to the system during startup. ~> $ echo $HOSTNAME mybox.mydomain.com
This variable identifies the hardware, it can be useful in determining which binaries to execute: ~> $ echo $HOSTTYPE x86_64
An array containing the numbers of groups the user is in: #!/usr/bin/env bash echo You are assigned to the following groups: for group in ${GROUPS[@]}; do IFS=: read -r name dummy number members < <(getent group $group ) printf "name: %-10s number: %-15s members: %s\n" &qu...
Contains the Internal Field Separator string that bash uses to split strings when looping etc. The default is the white space characters: \n (newline), \t (tab) and space. Changing this to something else allows you to split strings using different characters: IFS="," INPUTSTR="a,b,c...
Outputs the line number in the current script. Mostly useful when debugging scripts. #!/bin/bash # this is line 2 echo something # this is line 3 echo $LINENO # Will output 4
Similar to $HOSTTYPE above, this also includes information about the OS as well as hardware ~> $ echo $MACHTYPE x86_64-redhat-linux-gnu
OLDPWD (OLDPrintWorkingDirectory) contains directory before the last cd command: ~> $ cd directory directory> $ echo $OLDPWD /home/user
Returns information about the type of OS running on the machine, eg. ~> $ echo $OSTYPE linux-gnu
The search path for finding binaries for commands. Common examples include /usr/bin and /usr/local/bin. When a user or script attempts to run a command, the paths in $PATH are searched in order to find a matching file with execute permission. The directories in $PATH are separated by a : character...
The Process ID (pid) of the script or shell's parent, meaning the process than invoked the current script or shell. ~> $ echo $$ 13016 ~> $ echo $PPID 13015
PWD (PrintWorkingDirectory) The current working directory you are in at the moment: ~> $ echo $PWD /home/user ~> $ cd directory directory> $ echo $PWD /home/user/directory
The number of seconds a script has been running. This can get quite large if shown in the shell: ~> $ echo $SECONDS 98834

Page 1 of 2