Okay!! uptil now we have seen a lot of stuffs regarding Shell Script. But how about escape characters
Example: What if you want to display: Hi "Niraj" ?
So, if you type
sh-4.2 $ echo "Hi "Niraj""
The first and last " characters wrap the whole lot into one parameter passed to echo so that the spacing between the two word is kept as is.
But the above code would interpret as
Hello World
Please note here that we loose the double quotes entirely as they are not printed during this process.
Thus, if you want to display the quotes you are in great need of escape characters.
sh-4.2 $ echo "Hi \"Niraj\" "
Here the first Argument treated individual is
sh-4.2 $ echo *
first.sh second.sh etc.... will list files in the current directory.
sh-4.2 $ echo *txt
Will list all txt files in current directory
sh-4.2 $ echo "*"
Will print * as a literal / string
sh-4.2 $ echo "*txt"
*txt
However, " i.e. double quotes, $ i.e. dollar, ` backtick and \ backslash are still interpreted by the shell, even when they are in double quotes.
Thus for displaying the above characters we need a backslash (\) as a special character. Thus, they wont be interpreted by the shell, but passed as a string and not as a command or wild character.
Let us see if we want an output as below:
A sentence with ", backslash \, backtick ` and few spaces are alongwith dollar sign $
we would have to write a echo command as:
echo "
A sentence with \", backslash \\, backtick \` and few spaces are alongwith dollar sign \$"
Thus, backslash itself must be escaped to show that it is to be taken literally.
Regards,
Niraj Bhagchandani
Example: What if you want to display: Hi "Niraj" ?
So, if you type
sh-4.2 $ echo "Hi "Niraj""
The first and last " characters wrap the whole lot into one parameter passed to echo so that the spacing between the two word is kept as is.
But the above code would interpret as
- "Hello "
- World
- ""
Hello World
Please note here that we loose the double quotes entirely as they are not printed during this process.
Thus, if you want to display the quotes you are in great need of escape characters.
sh-4.2 $ echo "Hi \"Niraj\" "
Here the first Argument treated individual is
- " -> start of the string
- Hi
- \" is treated as "
- Niraj
- \" is again treated as "
- " -> end of the string
sh-4.2 $ echo *
first.sh second.sh etc.... will list files in the current directory.
sh-4.2 $ echo *txt
Will list all txt files in current directory
sh-4.2 $ echo "*"
Will print * as a literal / string
sh-4.2 $ echo "*txt"
*txt
However, " i.e. double quotes, $ i.e. dollar, ` backtick and \ backslash are still interpreted by the shell, even when they are in double quotes.
Thus for displaying the above characters we need a backslash (\) as a special character. Thus, they wont be interpreted by the shell, but passed as a string and not as a command or wild character.
Let us see if we want an output as below:
A sentence with ", backslash \, backtick ` and few spaces are alongwith dollar sign $
we would have to write a echo command as:
echo "
A sentence with \", backslash \\, backtick \` and few spaces are alongwith dollar sign \$"
Thus, backslash itself must be escaped to show that it is to be taken literally.
Regards,
Niraj Bhagchandani
0 comments:
Post a Comment