From 0b8ec70afa871d77b86501015793f155689b1fc3 Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 11 Apr 2020 22:59:21 +0000 Subject: deploy: f81b1f23a526450e16992e2e248495365d9e394d --- bash.html | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-) (limited to 'bash.html') diff --git a/bash.html b/bash.html index 3c937d9..9f47774 100644 --- a/bash.html +++ b/bash.html @@ -161,9 +161,55 @@

Parameter

# default param
-# if $foo set, then bar=$foo else bar=some_val
-bar=${foo:-some_val}
+bar=${foo:-some_val}  # if $foo set, then bar=$foo else bar=some_val
+
+# check param set
+bar=${foo:?msg}  # if $foo set, then bar=$foo else exit and print msg
+
+# indirect
+FOO=foo
+BAR=FOO
+bar=${!BAR}  # deref value of BAR -> bar=$FOO
+
+# prefix
+${foo#prefix}  # remove prefix when expanding $foo
+# suffix
+${foo%suffix}  # remove suffix when expanding $foo
+
+# substitute
+${foo/pattern/string}  # replace pattern with string when expanding foo
+# pattern starts with
+# '/'   replace all occurences of pattern
+# '#'   pattern match at beginning
+# '%'   pattern match at end
 
+
+

Note: prefix/suffix/pattern are expanded as pathnames.

+
+

Pathname

+
*           match any string
+?           match any single char
+\\          match backslash
+[abc]       match any char of 'a' 'b' 'c'
+[a-z]       match any char between 'a' - 'z'
+[^ab]       negate, match all not 'a' 'b'
+[:class:]   match any char in class, available:
+              alnum,alpha,ascii,blank,cntrl,digit,graph,lower,
+              print,punct,space,upper,word,xdigit
+
+

Wit extglob shell option enabled it is possible to have more powerful +patterns. In the following pattern-list is one ore more patterns separated +by | char.

+
?(pattern-list)   matches zero or one occurrence of the given patterns
+*(pattern-list)   matches zero or more occurrences of the given patterns
++(pattern-list)   matches one or more occurrences of the given patterns
+@(pattern-list)   matches one of the given patterns
+!(pattern-list)   matches anything except one of the given patterns
+
+
+

Note: shopt -s extglob/shopt -u extglob to enable/disable extglob +option.

+

I/O redirection

Note: The trick with bash I/O redirection is to interpret from left-to-right.

@@ -187,6 +233,37 @@ command 2>&1 >file
  • duplicate fd 1 to fd 2, effectively redirecting stderr to stdout
  • redirect stdout to file
  • +

    Completion

    +

    The complete builtin is used to interact with the completion system.

    +
    complete                    # print currently installed completion handler
    +complete -F <func> <cmd>    # install <func> as completion handler for <cmd>
    +complete -r <cmd>           # uninstall completion handler for <cmd>
    +
    +

    Variables available in completion functions:

    +
    # in
    +$1              # <cmd>
    +$2              # current word
    +$3              # privous word
    +
    +COMP_WORDS      # array with current command line words
    +COMP_CWORD      # index into COMP_WORDS with current cursor position
    +
    +# out
    +COMPREPLY       # array with possible completions
    +
    +

    compgen builtin is used to generate possible matches for word out of possible options. +The syntax is as follows:

    +
    compgen [option] [word]
    +
    +# usefule options:
    +
    +# -W <list>    compare against word-list
    +compgen -W "foo foobar bar" "f"
    +
    +# -d    compare against dir names
    +# -f    compare against file names
    +compgen -d -f "hom"
    +
    -- cgit v1.2.3