Get captured groups from a regex match against a string a='I am a simple string with digits 1234' pat='(.*) ([0-9]+)' [[ "$a" =~ $pat ]] echo "${BASH_REMATCH[0]}" echo "${BASH_REMATCH[1]}" echo …
Devamını Oku..Bash Script Debugging
Checking the syntax of a script with “-n” The -n flag enables you to check the syntax of a script without having to execute it: ~> $ bash -n testscript.sh …
Devamını Oku..Brace Expansion
Modifying filename extension $ mv filename.{jar,zip} This expands into mv filename.jar filename.zip . Create directories to group files by month and year $ mkdir 20{09..11}-{01..12} Entering the ls command will …
Devamını Oku..Customizing PS1
Colorize and customize terminal prompt This is how the author sets their personal PS1 variable: gitPS1(){ gitps1=$(git branch 2>/dev/null | grep '*') gitps1="${gitps1:+ (${gitps1/#\* /})}" echo "$gitps1" } #Please use …
Devamını Oku..Programmable completion
Simple completion using function _mycompletion() { local command_name="$1" # not used in this example local current_word="$2" local previous_word="$3" # not used in this example # COMPREPLY is an array which …
Devamını Oku..Process substitution
Compare two files from the web The following compares two files with diff using process substitution instead of creating temporary files. diff <(curl http://www.example.com/page1) <(curl http://www.example.com/page2) Feed a while loop …
Devamını Oku..Bash Script Scoping
Dynamic scoping in action Dynamic scoping means that variable lookups occur in the scope where a function is called, not where it is defined. $ x=3 $ func1 () { …
Devamını Oku..Bash Arithmetic
Parameter DetailsEXPRESSION Expression to evaluate Simple arithmetic with (( )) #!/bin/bash echo $(( 1 + 2 )) Output: 3 # Using variables #!/bin/bash var1=4 var2=5 ((output=$var1 * $var2)) printf "%d\n" …
Devamını Oku..Bash Script Math
Math using dc dc is one of the oldest programs on Unix. It uses reverse polish notation, which means that you first stack numbers, then operations. For example 1+1 iswritten …
Devamını Oku..Bash history substitutions
Quick Reference Interaction with the history # List all previous commands history # Clear the history, useful if you entered a password by accident history -c Event designators # Expands …
Devamını Oku..