From dc6d250f05294353c6f0986b1d55600a353722ba Mon Sep 17 00:00:00 2001 From: Jun Wei Woon Date: Thu, 9 Jul 2020 13:20:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=96=20Added=20more=20bash=20examples?= =?UTF-8?q?=20to=20cheatsheet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/en/posts/bash_cheatsheet.md | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/content/en/posts/bash_cheatsheet.md b/content/en/posts/bash_cheatsheet.md index c840a53..c7d56a7 100644 --- a/content/en/posts/bash_cheatsheet.md +++ b/content/en/posts/bash_cheatsheet.md @@ -192,8 +192,70 @@ function fun(){ echo fun } ``` +# String Manipulation +## Concatenate Strings +``` bash +string1=abc +string2=def +echo "$string1string2ghi jkl" +# Outputs: abcdefghi jkl +``` +## Extract String +``` bash +string="hello/devoalda/devo.com" + +# Get Filename +echo ${string##*/} +# Outputs: devo.com + +# Get Path +echo ${string%/*} +# Outputs: hello/devoalda + +# Get File Extension +echo ${string##*.} +# Outputs: com + +# Multiple Operations +NAME=${string##*/} # remove part before last slash +echo ${NAME%.*} # from the new var remove the part after the last period +# Outputs: devo +``` +## String contain substring + +{{< tabs bash python>}} + {{< tab >}} + +### bash + +``` bash +echo "This is a bash script" | grep "bash" +# True if "bash" is in sentence + +test='GNU/Linux is an operating system' +if [[ $test == *"Linux"* ]]; then + echo "true" +fi + +``` + + {{< /tab >}} + {{< tab >}} + +### python +``` python +test='GNU/Linux is an operating system' +if "Linux" in test: + print('true') +else: + print('false') +``` + {{< /tab >}} +{{< /tabs >}} + {{< expand "Credits/References" >}} [stackoverflow1](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) [stackoverflow2](https://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash) +[stackoverflow3](https://stackoverflow.com/a/19482947) {{< /expand >}}