String Variable, see also character
string in bash.
When calling a function, quote the variable otherwise bash will not see the string as atomic but as an array
output_args()
{
for arg
do
echo "$arg"
done
}
var="hello Nico"
echo "Without Quotes"
echo "**************"
output_args $var
echo
echo "With Quotes"
echo "**************"
output_args "$var"
Without Quotes
**************
hello
Nico
With Quotes
**************
hello Nico
#!/bin/sh
echo 'hello' > hello.txt
test=`cat hello.txt `
echo $test
hello
#!/bin/bash
value=$(<file.txt)
echo "$value"
The default for a string variable
var="My string"
A double-quoted string preceded by a dollar sign $“string” will cause the string to be translated according to the current locale.
If the current locale is C or POSIX, the dollar sign is ignored.
If the string is translated and replaced, the replacement is double-quoted.
From the bash man page, words of the form $'string' are treated specially.
The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
The expanded result is single-quoted, as if the dollar sign had not been present.
The below code will produce:
var=$'Hello\nWorld'
echo $var
Hello
World
BAR=$(printf "hello\nworld\n")
The escape character is a backslash \
Write the variable one after another:
var1=Hello
var2=Nico
echo $var1$var2
HelloNico
var1=Hello
var2=Nico
echo $var1 $var2
Hello Nico
var3="${var1} ${var2}"
echo $var3
Hello Nico
MY_VARIABLE=A
MY_VARIABLE+=B
echo $MY_VARIABLE
AB
variable="First"$'\n'
variable=$variable"Second"$'\n'
echo "$variable"
First
Second
echo -e 'One,Two,Three,Four\nOne,Two,Three,Four' | cut -d ',' -f 1-2,4 --output-delimiter=' '
One Two Four
One Two Four
Example:
UNKNOWN=unknown
echo ${UNKNOWN:2:3}
kno
echo ${UNKNOWN: -3} # the space is important, get the
echo "nicolas" | cut -c1-4
nico
echo -n "nicolas" | tail -c 3
echo -n "nicolas" | cut -c2- | rev | cut -c2- | rev
icola
With Parameter Expansion Removal operation, you can remove a part of the string.
MY_VARIABLE=123
echo ${MY_VARIABLE#1}
23
See also:
With length parameter expansion, you can get the length of a string variable.
MY_VARIABLE=123
echo ${#MY_VARIABLE}
3
# From upper to lower
echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'
# With a variable
X=$(echo "${X}" | tr '[:upper:]' '[:lower:]')
hello="HELLO WORLD"
echo ${hello,,} # Lowercase
echo ${hello,} # Lowercase First letter
echo ${hello^^} # Uppercase
echo ${hello^} # Uppercase First letter
Replacement with parameter expansion ?
${string/substring/replacement}
# Replacing all / in a path
foo=`pwd`
echo ${foo////_}
${string//substring/replacement}
${string/#substring/replacement}
${string/%substring/replacement}
word="hello/nico"
# Replacing the / character with a space
echo ${word//\// }
hello nico
See also:
regexp=$'[^ ]*\s(.*)' # Mandatory to put the regexp in a variable
if [[ "Hello Nico" =~ $regexp ]]; then echo "Match : First group is (${BASH_REMATCH[1]})"; else echo "No match"; fi;
Match : First group is (Nico)
IFS=', ' read -r -a array <<< "$string"
example
IFS=',' read -r -a myArray <<< "hello,nico"
echo ${#myArray[*]} # length of 2
echo ${myArray[*]}
hello nico
Name | Double Bracket operator | Single Bracket Operator | Example |
---|---|---|---|
Greater than | > | \> (*) | [[ a > b ]] || echo "a does not come after b" |
Lower than | < | \< (*) | [[ az < za ]] && echo "az comes before za" |
Equality | = (or ==) | = | [[ a = a ]] && echo "a equals a" |
Not Equal | != | != | [[ a != b ]] && echo "a is not equal to b" |
Pattern matching | = (or ==) | (not available) | [[ $name = a* ]] || echo "name does not start with an 'a': $name" |
RegularExpression matching | =~ | (not available) | [[ $(date) =~ ^Fri\ ...\ 13 ]] && echo "It's Friday the 13th!" |
Comparison operator in bash conditional expression for strings.
if [ "$a" = "$b" ]
# is not the same than (Why???)
if [ "$a"="$b" ]
if [ "$a" == "$b" ] # Synonym of ''=''
[[ $a == z* ]] # True if $a starts with a "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
[ $a == z* ] # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ] # The "<" char needs to be escaped in a [ construct
if [ "$a" != "$b" ]
Example:
Quote strings within test brackets as the variable may be not initialized
if [ -n "$string1" ]
then
echo "String \"string1\" is not null."
else
echo "String \"string1\" is null."
fi
Double Bracket operator | Single Bracket Operator | Example |
---|---|---|
-lt | -lt | [[ 8 -lt 9 ]] && echo "8 is less than 9" |
-ge | -ge | [[ 3 -ge 3 ]] && echo "3 is greater than or equal to 3" |
-gt | -gt | [[ 5 -gt 10 ]] || echo "5 is greater than than 10" |
-le | -le | [[ 3 -le 8 ]] && echo "3 is less than or equal to 8" |
-eq | -eq | [[ 5 -eq 05 ]] && echo "5 equals 05" |
-ne | -ne | [[ 6 -ne 20 ]] && echo "6 is not equal to 20" |
See equality operator above with the [[ ... ]] construct.
Example:
if [[ ! "${DIRECTORY_TO_CLEAN}" =~ ^(/opt/infa/|/home/).* ]] ; then
echo_err "Directory To Clean parameter must be a directory inside /opt/infa or /home/"
close_script 1
fi