-------------------------------------------------------------------------------- Shell variables -------------------------------------------------------------------------------- What is discussed here: ordinary variables, special variables and extracting selective parts of variable names. -------------------------------------------------------------------------------- I. Ordinary Variables. -------------------------------------------------------------------------------- A valid UNIX variable consists of alphanumeric and underscore. It does not include punctuations and BLANK. It starts with alpha or _ but not a digit The shell uses one (or more) blank spaces to separate the variables. Thus if you want to set a variable, e.g. A=file it is clear that you cannot have a blank space adorning the "=" signal. $ a1='fileSQ' #single quotea (useful if you do not wish to interpret meta-characters) $ b2="Hello Kitty" #double quote (useful if the name includes a blank) $ a="Hello_" $ echo a1 a1 #interpreted as a literal $ echo $a1 #usage of variable requires "$" fileSQ $ echo ${b2} #also another form of calling up a variable Hello Kitty $ echo $a2 #null (-> blank reutrn) since a2 is not defined $ echo ${a}2 #concatenation is the defaul operation Hello_2 $ echo ${#a1} #number of characters 6 $ a1_length=${#a1} $ echo $a1_length 6 ------------------------------------------------------------------------ II. Positional Parameter & Special Variables ------------------------------------------------------------------------ $0 the filename of the current script $# the number of arguments supplied to current script (string) $1,..,$9,${10},${11}... positional arguments in current script Position parameters are read only variables. Only "shift" and "set" can indirectly affect them. So, it is best to copy the position parameter to another variable and then manipulate that variable. $? exit status of last command (string) $$ process number of current shell (for shell it is their ID) $! process number of the last background command $* "$1c$2c$3.." where c is the first character of IFS variable $@ equivalent to "$1" "$2" "$3" ... See "special parameters". ------------------------------------------------------------------------ III. Extract various parts of a filename ------------------------------------------------------------------------ $ InFile="/Users/srk/Unix/Readme.txt" We now apply "regular"-like commands to extract bits of characters from the variable InFile "#" applies to the characters starting from the first character and working to the right % applies to the characters starting from the last character and working to the left The string manipulation operators are name#pattern ... removes shortest front-anchored pattern name##patetern ... removes longest front-anchored pattern name%pattern ... removes shortest rear-anchored pattern name%%pattern ... removes longest rear-anchored pattern name/pattern/string ... replace pattern by first string (note no closing "/") name//pattern/string ... replace all occurrences of pattern by string #goal: delete extension $ echo ${InFile%.*} %delete shortest stretch of characters from "." to $ /Users/srk/Unix/Readme #goal:extract extension $ echo ${Infile#*.} #extract extension txt $ echo ${Infile##*.} #this is prudent since there may be other "." in txt #the variable #get base filename $ echo ${InFile##*/} #delete longest strech of chars from ^ to "/" Readme.txt $ echo ${InFile##/*/} #maximal expansion, all characters between first "/" Readme.txt #and last "/" $ echo ${InFile/.dat/.rat} #notice there is no "/" at the end /Users/srk/Unix/Readme.rat $ number=" 25" #get rid of blank characters $ echo ${number// /} 25 #Less useful edits: #name .. number of characters in variable name:start_ind:nchar .. extract substring: from start_index (0,1,..) and of length nchar $ echo ${InFile:0:1} #note the second parameter is the number of chars / #and not an ending index $ echo ${InFile:0:2} /U