This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
bash [2011/10/13 12:59] k2patel [Bash FAQ] |
bash [2017/07/02 02:13] k2patel old revision restored (2013/05/08 17:56) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Bash ====== | ====== Bash ====== | ||
+ | * [[ Floating Point Calculation ]] | ||
+ | |||
==== Bash Short-Cut ==== | ==== Bash Short-Cut ==== | ||
Line 75: | Line 77: | ||
^abc^xyz Replace first occurance of abc with xyz in last command and execute it | ^abc^xyz Replace first occurance of abc with xyz in last command and execute it | ||
</code> | </code> | ||
+ | |||
+ | |||
+ | ==== Indirect Referance ==== | ||
+ | This is what i come across while writing some simple bash script.\\ | ||
+ | But it seems really usefull. so i am just adding here.\\ | ||
+ | |||
+ | <code bash | Example> | ||
+ | for inst in instance{1..10} | ||
+ | do | ||
+ | eval var1=\$$inst ## to evaluate $inst in assignment you have to use \$$inst | ||
+ | arry=($var1) | ||
+ | done | ||
+ | </code> | ||
+ | |||
+ | [[http://tldp.org/LDP/abs/html/ivr.html|Source]] | ||
==== If File Comparision ==== | ==== If File Comparision ==== | ||
Line 104: | Line 121: | ||
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers. | [ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers. | ||
[ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled. | [ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled. | ||
- | [ -z STRING ] True of the length if "STRING" is zero. | + | [ -z STRING ] True if the length of "STRING" is zero. |
[ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero. | [ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero. | ||
[ STRING1 == STRING2 ] True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance. | [ STRING1 == STRING2 ] True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance. | ||
Line 241: | Line 258: | ||
</code> | </code> | ||
+ | <code bash> | ||
+ | gobackto() { local p="$PWD"; while [[ $p ]]; do if [[ ${p##*/} = "$1" ]]; then cd "$p"; return; fi; p=${p%/*}; done; return 1; } | ||
+ | </code> | ||
==== Convert String to Array ==== | ==== Convert String to Array ==== | ||
Line 252: | Line 271: | ||
</code> | </code> | ||
+ | |||
+ | ==== Control case ==== | ||
+ | In Bash 4: | ||
+ | |||
+ | To lowercase | ||
+ | |||
+ | <code bash> | ||
+ | $ string="A FEW WORDS" | ||
+ | $ echo ${string,} | ||
+ | a FEW WORDS | ||
+ | $ echo ${string,,} | ||
+ | a few words | ||
+ | $ echo ${string,,[AEIUO]} | ||
+ | a FeW WoRDS | ||
+ | |||
+ | $ string="A Few Words" | ||
+ | $ declare -l string | ||
+ | $ string=$string; echo $string | ||
+ | a few words | ||
+ | </code> | ||
+ | |||
+ | To uppercase | ||
+ | |||
+ | <code bash> | ||
+ | $ string="a few words" | ||
+ | $ echo ${string^} | ||
+ | A few words | ||
+ | $ echo ${string^^} | ||
+ | A FEW WORDS | ||
+ | $ echo ${string^^[aeiou]} | ||
+ | A fEw wOrds | ||
+ | |||
+ | $ string="A Few Words" | ||
+ | $ declare -u string | ||
+ | $ string=$string; echo $string | ||
+ | A FEW WORDS | ||
+ | </code> | ||
+ | |||
+ | Toggle (undocumented) | ||
+ | |||
+ | <code bash> | ||
+ | $ string="A Few Words" | ||
+ | $ echo ${string~~} | ||
+ | a fEW wORDS | ||
+ | $ string="A FEW WORDS" | ||
+ | $ echo ${string~} | ||
+ | a fEW wORDS | ||
+ | $ string="a few words" | ||
+ | $ echo ${string~} | ||
+ | A Few Words | ||
+ | </code> | ||
+ | |||
+ | Capitalize (undocumented) | ||
+ | |||
+ | <code bash> | ||
+ | $ string="a few words" | ||
+ | $ declare -c string | ||
+ | $ string=$string | ||
+ | $ echo $string | ||
+ | A few words | ||
+ | </code> | ||
+ | |||
+ | Title case: | ||
+ | |||
+ | <code bash> | ||
+ | $ string="a few words" | ||
+ | $ string=($string) | ||
+ | $ string=${string[@]^} | ||
+ | $ echo $string | ||
+ | A Few Words | ||
+ | |||
+ | $ declare -c string | ||
+ | $ string=(a few words) | ||
+ | $ echo ${string[@]} | ||
+ | A Few Words | ||
+ | </code> | ||
+ | |||
+ | To turn off a declare attribute, use "+". For example, declare +c string. This affects subsequent assignments and not the current value. | ||
+ | |||
+ | ==== Bash Tips / Tricks ==== | ||
+ | |||
+ | === Quating === | ||
+ | Bash has a special form of quoting, $'string' in which backslash-character combinations are expanded. For example, echo $'this is a literal tab: \t' | ||
+ | <code bash> | ||
+ | re='foo.*bar'; [[ $'foo\nbar' =~ $re ]] && echo 'yes' || echo 'no' | ||
+ | </code> | ||
+ | |||
+ | === History Size === | ||
+ | <code bash> | ||
+ | HISTSIZE=1500000 | ||
+ | HISTFILESIZE=1500000 | ||
+ | HISTTIMEFORMAT="[%Y-%m-%d - %H:%M:%S] - " | ||
+ | </code> | ||
+ | |||
+ | === Truth Table === | ||
+ | <code bash> | ||
+ | These two commands not the same: | ||
+ | |||
+ | command1 && command2 || command3 | ||
+ | |||
+ | if command1 | ||
+ | then | ||
+ | command2 | ||
+ | else | ||
+ | command3 | ||
+ | fi | ||
+ | </code> | ||
+ | |||
+ | In the if-then-else, exactly one of command2 or command3 will be executed. But in the && || version, command3 runs if either command1 or command2 returns false. | ||
+ | |||
+ | <code bash> | ||
+ | for c1 in true false; do for c2 in true false; do echo "$c1 && $c2 || c3"; (echo c1; $c1) && (echo c2; $c2) || echo c3; done; done | ||
+ | </code> | ||
+ | |||
+ | And the output: | ||
+ | |||
+ | <code bash> | ||
+ | true && true || c3 | ||
+ | c1 | ||
+ | c2 | ||
+ | true && false || c3 | ||
+ | c1 | ||
+ | c2 | ||
+ | c3 | ||
+ | false && true || c3 | ||
+ | c1 | ||
+ | c3 | ||
+ | false && false || c3 | ||
+ | c1 | ||
+ | c3 | ||
+ | </code> | ||
+ | [[ http://www.linkedin.com/groups/Tuesdays-tip-difference-between-if-3716796.S.236851680?view=&srchtype=discussedNews&gid=3716796&item=236851680&type=member&trk=eml-anet_dig-b_pd-ttl-cn&ut=0FCRGmx_LdW5I1 | Link to Article ]] | ||
+ | ==== Networked Info ==== | ||
+ | |||
+ | * [[ Can we Hide Code ]] | ||
==== Bash FAQ ==== | ==== Bash FAQ ==== | ||