Skip to content

Instantly share code, notes, and snippets.

View mflorida's full-sized avatar
🛹
Shredding

Mark M. Florida mflorida

🛹
Shredding
View GitHub Profile
@mflorida
mflorida / pattern-matching.sh
Last active October 6, 2022 17:07 — forked from Error601/pattern-matching.sh
Bash string removal pattern matching samples
#!/bin/bash
# file path example
FILE=/home/user/src.dir/prog.c
echo ${FILE#/*/} # ==> user/src.dir/prog.c -- remove everything BEFORE the SECOND '/'
echo ${FILE##*/} # ==> prog.c -- remove part BEFORE LAST '/'
echo ${FILE%/*} # ==> /home/user/src.dir -- remove everything AFTER the LAST '/'
echo ${FILE%%/*} # ==> nil -- remove everything AFTER the FIRST '/' (returns empty string)
echo ${FILE%.c} # ==> /home/user/src.dir/prog -- remove everything AFTER '.c'
@magnetikonline
magnetikonline / README.md
Last active June 22, 2024 13:35
Bash string manipulation cheatsheet.

Bash string manipulation cheatsheet

Assignment
Assign value to variable if variable is not already set, value is returned.

Combine with a : no-op to discard/ignore return value.
${variable="value"}
: ${variable="value"}