rearrange orders

This commit is contained in:
bonnie 2020-07-19 21:15:06 +08:00
parent 6c083c518e
commit d7a768c446

257
README.md
View File

@ -130,6 +130,135 @@ $USER current username
$HOSTNAME current hostname
```
## Variable
[[back to top](#handy-bash-one-liners)]
##### Variable substitution within quotes
```bash
# foo=bar
echo "'$foo'"
#'bar'
# double/single quotes around single quotes make the inner single quotes expand variables
```
##### Get the length of variable
```bash
var="some string"
echo ${#var}
# 11
```
##### Get the first character of the variable
```bash
var=string
echo "${var:0:1}"
#s
# or
echo ${var%%"${var#?}"}
```
##### Remove the first or last string from variable
```bash
var="some string"
echo ${var:2}
#me string
```
##### Replacement (e.g. remove the first leading 0 )
```bash
var="0050"
echo ${var[@]#0}
#050
```
##### Replacement (e.g. replace 'a' with ',')
```bash
{var/a/,}
```
##### Replace all (e.g. replace all 'a' with ',')
```bash
{var//a/,}
```
```bash
#with grep
test="god the father"
grep ${test// /\\\|} file.txt
# turning the space into 'or' (\|) in grep
```
##### To change the case of the string stored in the variable to lowercase (Parameter Expansion)
```bash
var=HelloWorld
echo ${var,,}
helloworld
```
##### Expand and then execute variable/argument
```bash
cmd="bar=foo"
eval "$cmd"
echo "$bar" # foo
```
## Math
[[back to top](#handy-bash-one-liners)]
##### Arithmetic Expansion in Bash (Operators: +, -, *, /, %, etc)
```bash
echo $(( 10 + 5 )) #15
x=1
echo $(( x++ )) #1 , notice that it is still 1, since it's post-incremen
echo $(( x++ )) #2
echo $(( ++x )) #4 , notice that it is not 3 since it's pre-incremen
echo $(( x-- )) #4
echo $(( x-- )) #3
echo $(( --x )) #1
x=2
y=3
echo $(( x ** y )) #8
```
##### Print out the prime factors of a number (e.g. 50)
```bash
factor 50
# 50: 2 5 5
```
##### Sum up input list (e.g. seq 10)
```bash
seq 10|paste -sd+|bc
```
##### Sum up a file (each line in file contains only one number)
```bash
awk '{s+=$1} END {print s}' filename
```
##### Column subtraction
```bash
cat file| awk -F '\t' 'BEGIN {SUM=0}{SUM+=$3-$2}END{print SUM}'
```
##### Simple math with expr
```bash
expr 10+20 #30
expr 10\*20 #600
expr 30 \> 20 #1 (true)
```
##### More math with bc
```bash
# Number of decimal digit/ significant figure
echo "scale=2;2/3" | bc
#.66
# Exponent operator
echo "10^2" | bc
#100
# Using variables
echo "var=5;--var"| bc
#4
```
## Grep
[[back to top](#handy-bash-one-liners)]
@ -947,134 +1076,6 @@ case $type in
esac
```
## Variable
[[back to top](#handy-bash-one-liners)]
##### Variable substitution within quotes
```bash
# foo=bar
echo "'$foo'"
#'bar'
# double/single quotes around single quotes make the inner single quotes expand variables
```
##### Get the length of variable
```bash
var="some string"
echo ${#var}
# 11
```
##### Get the first character of the variable
```bash
var=string
echo "${var:0:1}"
#s
# or
echo ${var%%"${var#?}"}
```
##### Remove the first or last string from variable
```bash
var="some string"
echo ${var:2}
#me string
```
##### Replacement (e.g. remove the first leading 0 )
```bash
var="0050"
echo ${var[@]#0}
#050
```
##### Replacement (e.g. replace 'a' with ',')
```bash
{var/a/,}
```
##### Replace all (e.g. replace all 'a' with ',')
```bash
{var//a/,}
```
```bash
#with grep
test="god the father"
grep ${test// /\\\|} file.txt
# turning the space into 'or' (\|) in grep
```
##### To change the case of the string stored in the variable to lowercase (Parameter Expansion)
```bash
var=HelloWorld
echo ${var,,}
helloworld
```
##### Expand and then execute variable/argument
```bash
cmd="bar=foo"
eval "$cmd"
echo "$bar" # foo
```
## Math
[[back to top](#handy-bash-one-liners)]
##### Arithmetic Expansion in Bash (Operators: +, -, *, /, %, etc)
```bash
echo $(( 10 + 5 )) #15
x=1
echo $(( x++ )) #1 , notice that it is still 1, since it's post-incremen
echo $(( x++ )) #2
echo $(( ++x )) #4 , notice that it is not 3 since it's pre-incremen
echo $(( x-- )) #4
echo $(( x-- )) #3
echo $(( --x )) #1
x=2
y=3
echo $(( x ** y )) #8
```
##### Print out the prime factors of a number (e.g. 50)
```bash
factor 50
# 50: 2 5 5
```
##### Sum up input list (e.g. seq 10)
```bash
seq 10|paste -sd+|bc
```
##### Sum up a file (each line in file contains only one number)
```bash
awk '{s+=$1} END {print s}' filename
```
##### Column subtraction
```bash
cat file| awk -F '\t' 'BEGIN {SUM=0}{SUM+=$3-$2}END{print SUM}'
```
##### Simple math with expr
```bash
expr 10+20 #30
expr 10\*20 #600
expr 30 \> 20 #1 (true)
```
##### More math with bc
```bash
# Number of decimal digit/ significant figure
echo "scale=2;2/3" | bc
#.66
# Exponent operator
echo "10^2" | bc
#100
# Using variables
echo "var=5;--var"| bc
#4
```
## Time
[[back to top](#handy-bash-one-liners)]