Autosave: 2023-02-24 07:42:55
This commit is contained in:
parent
2bbd42f5de
commit
e8b832f160
2 changed files with 40 additions and 0 deletions
30
Programming_Languages/Shell/Strings_in_bash.md
Normal file
30
Programming_Languages/Shell/Strings_in_bash.md
Normal file
|
@ -0,0 +1,30 @@
|
|||
---
|
||||
categories:
|
||||
- Programming Languages
|
||||
tags:
|
||||
- shell
|
||||
- data-types
|
||||
---
|
||||
|
||||
# Strings in bash
|
||||
|
||||
## Return a substring by index
|
||||
|
||||
```bash
|
||||
myString="hello"
|
||||
substring=${myString:0:3}
|
||||
# hel
|
||||
```
|
||||
|
||||
This is often used when looping through each character in a string.
|
||||
|
||||
## Loop through each character in a string
|
||||
|
||||
```bash
|
||||
str="hallelujah"
|
||||
stringLength=$(expr length str)
|
||||
|
||||
for (( i=0; i<=${stringLength}; i++ )); do
|
||||
echo "${str:$i:1}"
|
||||
done
|
||||
```
|
10
Programming_Languages/Shell/Substrings_in_Bash.md
Normal file
10
Programming_Languages/Shell/Substrings_in_Bash.md
Normal file
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
categories:
|
||||
- Programming Languages
|
||||
tags:
|
||||
- shell
|
||||
|
||||
---
|
||||
|
||||
# Substrings in Bash
|
||||
|
Loading…
Add table
Reference in a new issue