bash
I collect here some pieces of usefull information about bash found elsewhere on internet.
Colon at the beginning of line
I found colon explained here - it means no-op command. Command itself is evaluated - or rather all expansions are proceeded, but command itself is not run.
It is commonly used to assign default values to variables. Braces here are part of parameter’s expansion - that is to precede expression with $. And meaning of := I found here.
= just stands for reassigment
: modify it to behave equally in cases variable is not set and variable has empty value.
: ${VAR1:=hello}
echo $VAR1
VAR2=hi
: ${VAR2:=goodmorning}
echo $VAR2
hello
hi
Use grep to find out match only
Grep command itself by default display all line where match occured. Here I learned about -o parameter what causes to output only what was matched.
-P is perl regular expression syntax
echo "ip adress is 1.2.3.4, really" | grep -o -P -e "(?:\d{1,3}\.){3}\d{1,3}"
1.2.3.4
Sent to stdin
I found some interesting things about stdin [here] [send string to stdin] and here. So you can use <<<, <(...) to redirect output of command - and there are no spaces after redirection and pipe |.
cat <<< "This is coming from the stdin"
cat <(ls ~/test1) <(ls ~/test2)
echo hello | cat
This is coming from the stdin
filex_intest1
filey_intest2
hello
One difference(but don’t know details) is you can fetch file directly to loop
while read line
do
echo $line
done < ~/test1/filex_intest1
But when used whith pipe you need command to sent content of file
cat ~/test1/filex_intest1 | while read line
do
echo $line
done
Output is same in both cases. Output contains lines ending with EOL. If last row do not end with EOL it won’t be printed.
line one
line two
Evaluate regular expression
Evaluate regular expression is one part of evaluate expression. Syntax of regular expressions here is gnu basic regular expression and that is why regular expression looks so akwardly. Many characters just need to be escaped. Expression is searched from begin(like beeing prepend with ^) and when matching group is used it will return match for this group. Only first group behaves like this.
expr 'ip adress is 1.2.3.4, really' : '.*\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)'
1.2.3.4