add variable section

bash variable can be frustrating
This commit is contained in:
onceupon 2018-04-10 10:10:26 +08:00 committed by GitHub
parent 6a1ec40741
commit cfa3d59e92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ http://onceupon.github.io/Bash-Oneliner/
- [Xargs](#xargs) - [Xargs](#xargs)
- [Find](#find) - [Find](#find)
- [Loops](#loops) - [Loops](#loops)
- [Variable](#variable)
- [Math](#math) - [Math](#math)
- [Download](#download) - [Download](#download)
- [Random](#random) - [Random](#random)
@ -816,7 +817,35 @@ for i in $(cat tpc_stats_0925.log |grep failed|grep -o '\query\w\{1,2\}');do cat
for line in $(cat myfile); do echo $line; read -n1; done for line in $(cat myfile); do echo $line; read -n1; done
``` ```
## Variable
[[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)]
##### variable substitution within quotes
```bash
# foo=bar
echo "'$foo'"
#'bar'
# double/single quotes around single quotes make the inner single quotes expand variables
```
##### replacement (e.g. replace 'a' with ',')
```bash
{i/a/,}
```
##### replace all (e.g. replace all 'a' with ',')
```bash
{i//a/,}
```
#with grep
```bash
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 lower case (Parameter Expansion)
```bash
var=HelloWorld
echo ${var,,}
helloworld
```
## Math ## Math
[[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] [[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)]
@ -2024,23 +2053,6 @@ Ctrl+r
python -m SimpleHTTPServer python -m SimpleHTTPServer
``` ```
##### Variables
```bash
{i/a/,}
```
e.g. replace all
```bash
{i//a/,}
```
//for variable i, replace all 'a' with a comma
e.g. with grep
```bash
test="god the father"
grep ${test// /\\\|} file.txt
# turning the space into 'or' (\|) in grep
```
##### Read user input ##### Read user input
```bash ```bash