Bash: String Variables
String variables are used to store and manipulate text in Bash scripts.
Defining String Variables
Basic variable definition
my_string="Hello World" To define a string variable, use name="value". Note that there should be no spaces around the equals sign.
Accessing and Printing String Variables
Accessing a variable
echo $my_string To use the value stored in a variable, precede its name with a dollar sign $. The echo command prints the value to the terminal.
Concatenating Strings
Combining variables and strings
full_name="John Doe" echo "My name is $full_name"Bash automatically concatenates adjacent strings and variables. You can combine multiple variables or mix them with regular text inside double quotes.
Making a Substring
Extracting a substring from a string variable
my_string="Hello World"
echo ${my_string:0:5} # Hello To extract a substring from a string variable, use ${variable_name:start:length}. This returns a portion of the string starting at the specified index and with the specified length.
Extracting a substring from the end of a string variable
my_string="Hello World"
echo ${my_string: -5} # World To extract a substring from the end of a string variable, use a negative start index. This returns the specified number of characters from the end of the string.
Extracting a substring starting from a specific index to the end of the string
my_string="Hello World"
echo ${my_string:6} # World If you omit the length parameter, the substring will include all characters from the start index to the end of the string.
Extracting a substring using a variable for the start index
my_string="Hello World"
start_index=6
echo ${my_string:start_index:5} # World You can also use a variable to specify the start index when extracting a substring. Just ensure that the variable is defined and contains a valid index before using it in the substring syntax.
Extracting a substring starting from a specific index with negative length
my_string="Hello World"
start_index=6
echo ${my_string:start_index:0:-6} # Hello To extract a substring starting from a specific index up to a specific index from the end of the string, use the syntax ${variable_name:start:0:-length_from_end}. This allows you to specify how many characters to exclude from the end of the string.
Extracting a substring using a variable for the length
my_string="Hello World"
length=5
echo ${my_string:0:length} # Hello Similarly, you can use a variable to specify the length of the substring. This allows for dynamic substring extraction based on variable values.
Getting Length of a String
Find the length of a string variable
my_string="Hello World"
echo ${#my_string} To get the length of a string stored in a variable, use ${#variable_name}. This returns the number of characters in the string.
Splitting a String into an Array
Splitting a string by a delimiter
my_string="apple,banana,cherry"
IFS=',' read -ra fruits <<< "$my_string"
echo "${fruits[0]}" # apple
echo "${fruits[1]}" # banana To split a string into an array, set the IFS (Internal Field Separator) to your delimiter and use read -ra to assign the words read to sequential indices of the array fruits.
Cast string to number
Using arithmetic expansion
my_string="42"
number=$((my_string + 0))
echo $number # 42 To cast a string to a number, you can use arithmetic expansion. By adding 0 to the string variable, Bash will attempt to convert it to a number. If the string is a valid number, it will be converted successfully; otherwise, it will result in an error or unexpected behavior.
Exporting String Variables
The export keyword
export my_variable="Some Value" The export keyword makes a variable available to child processes or scripts. Without export, a variable is only available in the current shell session.