Quoting
Double Quotes
Let's say we want we assign a string with spaces to a variable. If we try:
var=This is a test
We get an error:
$ var1=This is a test.
-bash: is: command not found
The Shell assigns "This" to the variable and thinks the next word is a command.
To accomplish this we can surround the text with quotes.
ajay.mittal@AMITTAL-NB03 ~/temp
$ var1="This is a test."
We can get the value of the string variable out by
enclosing it in double quotes.
$ echo $var1
This is a test.
We might have situations in which we need to place
some text right after "$var1". If we do:
$ var1="Hello"
$ echo $var1World
We may get a blank line as the output. We can use the curly
braces and then put the word.
$ echo ${var1}World
HelloWorld
Double quotes will not ignore dollar signs , back quotes or backslashes.
ajay.mittal@AMITTAL-NB03 ~/temp
$ var2="This is var1:$var1"
ajay.mittal@AMITTAL-NB03 ~/temp
$ echo $var2
This is var1:This is a test.
Using the back quote:
Back Quotes
Command Substitution
[amittal@hills ~]$ var1=`date`
[amittal@hills ~]$ echo $var1
Tue Oct 30 17:42:18 PDT 2018
Using the back quote character we can execute the command inside and
the use the output. In the above the "date" command is executed and
the output is placed in the variable "var1" . We can confirm that by
echoing the contents of the variable "var1" .
ajay.mittal@AMITTAL-NB03 ~/temp
$ var1=" `date` is todays date"
ajay.mittal@AMITTAL-NB03 ~/temp
$ echo $var1
Sun, Oct 28, 2018 6:15:10 PM is todays date
The backquote indicates execution of a command and is interpreted by
the shell.
Single Quotes
Single quotes are more restrictive. They treat the string as it is
without any interpretation.
[amittal@hills sed]$ var1="Some value"
[amittal@hills sed]$ echo '$var1'
$var1
The value of the variable "var1" is not printed out.
Backslash
We can use the backslash to assign and remove special meaning to characters .
[amittal@hills sed]$ echo "a a" | grep "\s"
a a
[amittal@hills sed]$ echo "aa" | grep "\s"
The above states that "grep" should look for the "space" character .
[amittal@hills sed]$ echo "aa^" | grep "\^"
aa^
The above looks for the carat character . With the backslash we are
actually looking at the character caret instead of looking for the
pattern at the beginning of the string.
Strings
We look at some of the string operations.
Concatenation
There is no special operator like "+" or "."
For literal strings we can place the strings side by side.
$ var3="Global""Warming"
$ echo $var3
GlobalWarming
Note we cannot do:
$ var3="Global" "Warming"
sh: Warming: command not found
The shell assigns "Global" to var3 and think that "Warming" is another command..
If we want a space in between we can do:
$ var3="Global"" ""Warming"
$ echo $var3
Global Warming
For variables we can place the variables in
a string surrounded by double quotes.
$ var1="Global"
$ var2="Warming"
$ echo "$var1 $var2"
Global Warming
Length
We can also get the length of a string.
str="Unix Shell"
$ echo ${#str}
10
SubStrings
str="Roadstar"
echo ${str:0:4}
#Start from 0 and include 4 characters
echo ${str:5}
Replace
text="I like shell scripting. I like to automate tasks."
# Replace only the first 'apple'
echo "${text/like/adore}"
$ echo "${text/like/adore}"
I adore shell scripting. I like to automate tasks.
#Replace it in the whole line
$ echo "${text//like/adore}"
I adore shell scripting. I adore to automate tasks.