Profile_bird

Hey there! bashcookbook is using Twitter.

Twitter is a free service that lets you keep in touch with people through the exchange of quick, frequent answers to one simple question: What are you doing? Join today to start receiving bashcookbook's tweets.

Already using Twitter
from your phone? Click here.

bashcookbook

  1. bash has 4 prompt strings; PS2 is used when you haven't completed your input in response to the initial prompt (PS1), on the command line.
  2. With a select, the actual input is in the variable "REPLY" but the matching value is in the variable you supplied on the select command.
  3. "select VAR in x y z ; do ..." displays your choices: "1) x 2) y 3) z" and prompts (PS3) for input. If you type 2 then VAR is set to "y"
  4. Running a "select" in your script prints the prompt "#?", the value of PS3, one of 4 prompt variables in bash. The usual prompt is in PS1.
  5. Make simple text-based menus in your script w/ select. Ex: select VAR ; do ls -ld "$VAR" ; done Save it as "ils" Run it: bash ils *
  6. Also new in bash 4.0: a case can end with ";;&" not just ";;" or ";&" - it continues testing other case patterns for more matches.
  7. New in bash 4.0, a case can end with ";&" instead of ";;" and it executes the statements of the next case, too (regardless of its pattern).
  8. You can have a space in your case pattern but you need to escape it, e.g.: *\ *) echo spacey ;; so that bash knows it's all one pattern.
  9. There is no "default" case, but using *) as the last case will accomplish just that - since "*" matches anything. Be sure it's the last one!
  10. Your "case" patterns can be in parens ( *.c ) or just end with right paren: *.c ) The left paren is allowed but optional.
  11. Want multiple patterns in one case? Use an or-bar "|" between each pattern. Example: *.[ch] | *.java ) echo source;;
  12. Each case in a case statement is bash pattern matching, not regular expressions, then a ')' then statements then ';;'
  13. bash case example: case "$FILENM" in *.jpg) echo image ;; *.[ch]) echo C-lang ;; *) echo unknown;; esac (ugly as 1 line; tweet fm'ts R ltd.)
  14. Whereas C and perl have a "switch" statement with each value or pattern preceded by a "case" keyword, bash calls it a "case" statement.
  15. An advantage to using [[ ]] for an if statement: you have the =~ operator which matches regular expressions. (avail. in bash 3.0 and newer)
  16. commands inside "( )" run in a subshell; ( cd /tmp; X=7; other_stuff; ) after that, U R back to prev. directory, prev. value of X.
  17. 1 other use of ":", since it returns 0, use it like "true" as "while : loop ; do ... done" where "loop", an arg to ":", is just a comment.
  18. : <<'END' with lots of text after it, as comments, w/o needing a leading "#". The ":" does nothing, but << swallows the text up to END.
  19. We just reached 800 followers! Hurray!! Since it's Follow Friday suggest @bashcookbook to others and let's aim for 1,000.
  20. ":" is a "no-op" (does nothing) but bash expands any arguments and does any i/o redirections; a useful (?) example next time! (hint: p.83)