String Manipulation in Bash – Slice, Replace & Transform Without External Commands!
Need to process text in Bash without calling sed, awk, or cut? In this video, we'll show you how to use Bash's built-in string manipulation operators to slice, replace, trim, and transform strings directly in your scripts. Whether you're extracting file extensions, parsing emails, or formatting output, these native features are fast, portable, and perfect for efficient shell scripting. No external dependencies—just pure Bash! Learn: ✅ Step-by-Step Guide: 1.String Length: text="Hello World" echo ${#text} # Output: 11 2.Extract Substring: text="Hello World" echo ${text:0:5} # Output: Hello (positions 0-4) echo ${text:6:5} # Output: World (positions 6-10) echo ${text:6} # Output: World (from position 6 to end) 3.Replace Text: text="Hello World" echo ${text/World/Linux} # Replace first: "Hello Linux" echo ${text//o/O} # Replace all: "HellO WOrld" 4.Remove Parts (Prefix/Suffix): filename="archive.tar.gz" # Remove from beginning (shortest/longest match) echo ${filename#*.} # Output: tar.gz echo ${filename##*.} # Output: gz # Remove from end (shortest/longest match) echo ${filename%.*} # Output: archive.tar echo ${filename%%.*} # Output: archive 5.Change Case: text="Hello World" echo ${text^^} # UPPERCASE: HELLO WORLD echo ${text,,} # lowercase: hello world echo ${text^} # First letter uppercase: Hello World echo ${text,} # First letter lowercase: hello World 6.Default Values: name=${1:-"Guest"} # Use "Guest" if $1 is empty echo "Hello $name" set -- "World" # Set positional parameter for testing name=${1:-"Guest"} echo "Hello $name" # Output: Hello World 7.Real-World Examples: # Get file extension file="script.sh" ext=${file##*.} echo "Extension: $ext" # Output: sh # Get filename without extension base=${file%.*} echo "Name: $base" # Output: script # Extract domain from email email="[email protected]" domain=${email#*@} echo "Domain: $domain" # Output: example.com # Capitalize first letter text="hello world" echo ${text^} # Output: Hello world echo ${text^^} # Output: HELLO WORLD ✅ Why Use Bash String Manipulation? Zero Dependencies: No need for sed, awk, or cut—pure Bash. Performance: Built-in operators are faster than spawning external processes. Portability: Works in any POSIX-compatible shell with Bash extensions. Script Readability: Keep logic inline without complex pipelines. ✅ Pro Tips: Remember: ${var#pattern} removes shortest match from start; ${var##pattern} removes longest. Use ${var%pattern} and ${var%%pattern} similarly for suffix removal. Test string operations with echo before using in destructive scripts. Combine with parameter expansion: ${file%.*}.${ext} to rebuild filenames. Use ${var:-default} to safely handle empty variables in scripts. For case conversion, ^^ and , require Bash 4+—check version with bash --version. You can find the full tutorial, script examples, and practice exercises here: 📁 https://gitlab.com/hatem-badawi/linuxhowto/-/tree/main/bash/bash-string-manipulation Perfect for Bash scripters who want to write cleaner, faster, and more portable shell code! Hit subscribe for more command-line tips and like if this helped. Let us know: Which string operator do you use most often? 👉 Watch now and master text processing in pure Bash! #LinuxTips #BashScripting #StringManipulation #CommandLine #Automation #ShellScripting #SysAdmin (Short, clear, and packed with practical knowledge!)
Download
0 formatsNo download links available.