Skip to content

Latest commit

 

History

History
139 lines (118 loc) · 3.05 KB

strings.md

File metadata and controls

139 lines (118 loc) · 3.05 KB

Below are some common bash string operations:

  1. String Length Get the length of a string.
string="Hello, World!"
length=${#string}
echo $length  # Output: 13
  1. Substring Extraction Extract a substring from a string.
string="Hello, World!"
substr=${string:7:5}
echo $substr  # Output: World
string:7:5 extracts a substring starting from index 7 and of length 5.
  1. Substring Replacement Replace the first occurrence of a substring.
string="Hello, World!"
new_string=${string/World/Bash}
echo $new_string  # Output: Hello, Bash!

Replace all occurrences of a substring.

string="apple apple apple"
new_string=${string//apple/orange}
echo $new_string  # Output: orange orange orange
  1. Prefix and Suffix Removal Remove a prefix.
filename="photo.jpg"
new_filename=${filename#photo}
echo $new_filename  # Output: .jpg

Remove a suffix.

filename="photo.jpg"
new_filename=${filename%.jpg}
echo $new_filename  # Output: photo
  1. Pattern Matching in Substrings Remove the shortest match of a pattern from the beginning.
string="Hello, World!"
new_string=${string#Hello, }
echo $new_string  # Output: World!

Remove the longest match of a pattern from the beginning.

string="abc/def/ghi"
new_string=${string##*/}
echo $new_string  # Output: ghi

Remove the shortest match of a pattern from the end.

string="file.tar.gz"
new_string=${string%.gz}
echo $new_string  # Output: file.tar

Remove the longest match of a pattern from the end.

string="file.tar.gz"
new_string=${string%%.*}
echo $new_string  # Output: file
  1. Concatenation Concatenate strings.
str1="Hello"
str2="World"
concatenated="$str1, $str2!"
echo $concatenated  # Output: Hello, World!
  1. Convert to Upper/Lower Case Convert to lowercase.
string="HELLO"
lower_string=${string,,}
echo $lower_string  # Output: hello

Convert to uppercase.

string="hello"
upper_string=${string^^}
echo $upper_string  # Output: HELLO
  1. Default Value Substitution Use a default value if the string is empty.
string=""
echo ${string:-"Default Value"}  # Output: Default Value
  1. Trim Leading/Trailing Whitespace There is no direct Bash operation for trimming whitespace, but you can use parameter expansion and sed or awk:
string="  Hello, World!  "
trimmed_string=$(echo "$string" | xargs)
echo ">$trimmed_string<"  # Output: >Hello, World!<

Converting an Integer to a String
In Bash, integers are stored as strings by default. When you assign an integer value to a variable, it is treated as a string unless you explicitly use it in a mathematical context.

num=123  # This is an integer stored as a string

# Using the integer as a string
string="Number: $num"
echo $string  # Output: Number: 123

Converting a String to an Integer
If you need to perform arithmetic operations, Bash automatically treats numeric strings as integers.

string="123"

# Converting string to integer by using it in an arithmetic context
num=$((string + 1))
echo $num  # Output: 124