Bash programming

Still under construction

Unordered list of topics to program in Bash

Shell expansions

http://wiki.bash-hackers.org/syntax/pe
https://opensource.com/article/17/6/bash-parameter-expansion

Braces expansion

   echo a{d,c,b}e               --> Outputs "ade ace abe"  (in one single line)    
   mkdir ./{old,new,dist,bugs}  --> Creates old, new, dist and bugs directories    
   rm -r ./{old,new,dist,bugs}  --> Deletes old, new, dist and bugs directories    

Can also use wildcards

  chown root /test/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}   

Parameter expansion

Use a default value:

${parameter:-word}

If the parameter is unset or null, this one EXPANDS to WORD, otherwise it expands to the value of PARAMETER but it only happens that time. WORD is not assigned (so not set) to parameter

Assign a default value:

${parameter:=word}

If the parameter is unset or null, this one ASSIGNS to WORD, otherwise it expands to the value of PARAMETER. Once assigned, WORD is assigned (so set) to parameter and useful afterwards

With the ‘:‘, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

mycomputer ~ $ parm1=                        # parm1 creater empty
mycomputer ~ $ echo $parm1

mycomputer ~ $ echo ${parm1-"kkkk"}          # No ':'    -->  only checks existence
                                             # As parm1 exists, does not continue
mycomputer ~ $ echo ${parm1:-"kkkk"}         # With ':'  --> will check existence and null value
kkkk                                         # As parm1 exists and has null value, expands to 'kkkk'
mycomputer ~ $ echo $parm1

mycomputer ~ $ echo ${parm1:="kkkk"}
kkkk
mycomputer ~ $ echo $parm1
kkkk

String length:

 ${#parameter}     -->   String length
    $ MYSTRING="Be water my friend"
    $ echo ${#MYSTRING}
    18

Positional parameters shift

 ${@:length}            --> Length positional parameters beginning at offset
    $ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
    $ echo ${@:7}
    7 8 9 0 a b c d e f g h

Substrings

${parameter#word}
${parameter##word}            

Word is expanded to produce a pattern just as in filename expansion. If the pattern matches the beginning of the expanded value of parameter then the result of the expansion is the expanded value of parameter with the shortest (#) or the longest (##) matching pattern deleted.

${parameter%word}     
${parameter%%word}     

Word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest (%) or the longest (%%) matching pattern deleted.

mycomputer ~ $ fitxer="ferran.com.out"
mycomputer ~ $ echo ${fitxer#*.}
com.out
mycomputer ~ $ echo ${fitxer##*.}
out
mycomputer ~ $ echo ${fitxer%.*}
ferran.com
mycomputer ~ $ echo ${fitxer%%.*}
ferran

FOR loops

With a range in curly brackets, will loop all range. Lasts versions of bash admit a step value.

mycomputer ~ $ for a in {1..5}; do echo $a; done
1
2
3
4
5
mycomputer ~ $ for a in {1..5..2}; do echo $a; done
1
3
5