Translate

Archives

The Time Keyword in Bash

The word time is one of the bash shell reserved words. It is not a bash shell builtin. $ builtin time bash: builtin: time: not a shell builtin Bash does support the older Bourne shell keyword times as a builtin. This builtin prints out the user and system times used by the shell and its children. So where is the time reserved word used in the bash shell? It is primarily intended for printing pipeline timing statistics. From the current online bash documentation: The format for a pipeline is [time [-p]] [!] command1 [| command2 …] The output of each

Shell String Concatenation

Many people are unaware that string concatenation using the += syntax is fully supported in modern versions of both the Bash and Korn shells Here is a simple example which demonstrates the syntax: str=”Hello” $ str+=” ” $ str+=”World” $ echo $str Hello World $ If you use this syntax with “numbers”, you end up with a string that looks like a number but is not a number. $ a=2 $ a+=4 $ echo $a $ 24

Differences in Variable Scope in Shell Functions

Ksh93 and bash have subtly different scopes for variables defined in shell functions as the following example shows: # POSIX function syntax testme2() { printf “Function testme2 invokedn” var21=testme21 typeset var22=testme22 } function testme1 { printf “Function testme1 invokedn” var11=testme11 typeset var12=testme12 } testme1 echo “VAR11=$var11” echo “VAR12=$var12” Here is the output when run under ksh93: Function testme1 invoked VAR11=testme11 VAR12= Function testme2 invoked VAR21=testme21 VAR22=testme22 and here is the output when run under bash: Function testme1 invoked VAR11=testme11 VAR12= Function testme2 invoked VAR21=testme21 VAR22= Note the different output for var22! Ksh93 has lexical scoping. A variable is normally global

Implementing strstr in Korn Shell

Neither Bash or the Korn Shell 93 (ksh93) provide a C-like strstr builtin. Here is a simple implementation of a strstr function in ksh93. The strstr function finds the first occurrence of s2 in s1. If s2 is an empty string, 0 is returned; if s2 occurs nowhere in s1, 0 is also returned; otherwise the offset to the first character of the first occurrence of s2 is returned. #!/bin/ksh function strstr { typeset s1=”$1″ typeset s2=”$2″ if [[ ${#s2} == 0 ]] then return 0 fi typeset len=${#s1} typeset first=${s1%%${s2}*}x typeset ndx=${#first} if (( ndx > len )) then

Bash Hash Builtin

The Bash shell hash builtin maintains a hash cache (table) containing the full pathname of previously executed commands that are on your PATH environmental variable. If the command is in the hash table, it just executes it without searching for it in the various path components. SYNTAX hash [-r] [-p filename] [name] OPTIONS -r Reset (causes the shell to forget all remembered locations) -p Use filename as the location of name (don’t search $PATH) If no arguments are given, information about remembered commands is printed. The return status is zero unless a name is not found or an invalid option