Autosave: 2023-02-04 09:26:34
This commit is contained in:
parent
e982d1a6ab
commit
f475ad9be2
2 changed files with 31 additions and 2 deletions
15
Programming_Languages/Shell/Loops_in_bash.md
Normal file
15
Programming_Languages/Shell/Loops_in_bash.md
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags:
|
||||||
|
- shell
|
||||||
|
---
|
||||||
|
|
||||||
|
# Loops in bash
|
||||||
|
|
||||||
|
```bash
|
||||||
|
for element in "${arr[@]}"
|
||||||
|
do
|
||||||
|
echo "$element"
|
||||||
|
done
|
||||||
|
```
|
|
@ -9,8 +9,7 @@ tags:
|
||||||
|
|
||||||
## `readarray`
|
## `readarray`
|
||||||
|
|
||||||
`readarray` makes it really easy to split input into an array.
|
`readarray` makes it really easy to split input into an array based on new lines.
|
||||||
|
|
||||||
Say we have this file as input:
|
Say we have this file as input:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -48,3 +47,18 @@ is
|
||||||
> The _-t_ flag removes the trailing newline
|
> The _-t_ flag removes the trailing newline
|
||||||
|
|
||||||
Add more: https://linuxhint.com/split-string-array-bash/
|
Add more: https://linuxhint.com/split-string-array-bash/
|
||||||
|
|
||||||
|
## `read`
|
||||||
|
|
||||||
|
For different delimiters we have to use `read`, combined with `IFS` the **Internal Field Separator**.
|
||||||
|
|
||||||
|
For example, to split by comma:
|
||||||
|
|
||||||
|
```plaintext
|
||||||
|
# comma-input.txt
|
||||||
|
something, something else, something more
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
IFS=',' read -a arr < ./comma_inputs.txt
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue