Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX Shell reference mechanism


May 23, 2021 UNIX Getting started


Table of contents


Shell reference mechanism

Meta-characters

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).

Example

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

Single quotes

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'

Double quotes

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:

  • The $ parameter is replaced.
  • The back quote used for command replacement.
  • Make the dollar logo literally display.
  • Make the back quotes literally appear.
  • Enable embedded double quotes.
  • Enable embedded backslashes.
  • All other characters are literally displayed (not in special sense).

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'

Inverse quotes

Any Shell command placed between back quotes executes the command

Grammar

Here's a simple syntax to put any shell command between back quotes:

Example

    var=`command`

Example

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