May 23, 2021 UNIX Getting started
UNIX Shell provides a variety of meta-characters of special significance while taking advantage of them in any shell script and resulting in the termination of a word in addition to a reference.
For example, when listing a directory in a file? /b10>Matches a single character, and matches more than one character. Here's a list of shell special characters, also known as meta-characters:
* ? [ ] ' " \ $ ; & ( ) | ^ < > new-line space tab
Use before a character, which may be referenced (for example, on its own behalf).
The following example shows how to print s or ? :
#!/bin/sh
echo Hello; Word
This will produce the following results.
Hello
./test.sh: line 2: Word: command not found
shell returned 127
Now, let's try the reference character:
#!/bin/sh
echo Hello\; Word
This results in the following:
Hello; Word
The $ symbol is a meta-character, so it must be referenced to avoid being specially handled by the shell:
#!/bin/sh
echo "I have \$1200"
This results in the following:
I have $1200
is a reference in four forms:
Quoting | Description |
---|---|
Single quotes | All special characters between these quotation marks lose their special meaning |
Double quotes |
All special characters between these quotation marks lose their special meaning except for the following characters:
- $ - ` - \$ - \' - \" -\\ |
Backslash | Any character that follows the backslash directly loses their special meaning |
Inverse quotes | All special characters between these quotation marks are executed as commands |
Consider echo commands that contain many special shell characters:
echo <-$1500.**>; (update?) [y|n]
Adding a backslash before each particular character can be extremely cumbersome and difficult to read:
echo \<-\$1500.\*\*\>\; \(update\?\) \[y\|n\]
There is an easy way to refer to a large set of characters. Place a single quote (') at the beginning and end of the string:
echo '<-$1500.**>; (update?) [y|n]'
Any character in single quotes is referenced just as each character is forwarded with a backslash. So, this echo command will now be displayed correctly.
If you want to output a single quote within a string, you should not place the entire string in single quotes, but instead you should use a backslash in front of a single quote.
echo 'It\'s Shell Programming'
Try the following shell script. This shell script uses single quotes:
VAR=ZARA
echo '$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]'
This outputs the following results:
$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]
So that's not what you want to show. O bviously, single quotes prevent variables from being replaced. If you want to replace variable values and make quotation marks work as expected, you need to place the command in double quotes, as follows:
VAR=ZARA
echo "$VAR owes <-\$1500.**>; [ as of (`date +%m/%d`) ]"
This results in the following:
ZARA owes <-$1500.**>; [ as of (07/02) ]
Double quotes give all characters a special meaning except for the following:
Any character in single quotes is referenced just as each character is forwarded with a backslash. So, this echo command will now be displayed correctly.
If you want to output a single quote within a string, you should not place the entire string in single quotes, but instead you should use a backslash before a single quote.
echo 'It\'s Shell Programming'
Any Shell command placed between back quotes executes the command
Here's a simple syntax to put any shell command between back quotes:
var=`command`
The date command is executed below and the results are stored in the DATA variable.
DATE=`date`
echo "Current Date: $DATE"
This outputs the following results:
Current Date: Thu Jul 2 05:28:45 MST 2009