Escape and command substitution in scripting

I have one question in shell script for escape "\" with command substitution "` `". I post there to seek help to understand how it works.

My original one piece of code in script like this: This piece of code in whole script is working without errors

chk_mode=`sqlplus -s /nolog<<EOF
connect / as sysdba
set pagesize 0 feedback off heading off
select open_mode from v\\\$database;
EOF`

Usually if I want to escape $ sign from v$database, I should use "v\$database". Why is this piece of code using 3 backslash as v\\\$database? Is this because the backslash is in command substitution(backtick)? I tested in command line: only v\$database works, v\\\$database is not working. My Unix server is solaris 11.3 and SHELL is bash.

Please help me to understand why using 3 backslashes as escape. Thanks for your advice.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

3 Likes

Yes it is because the command is backticks. This is one of the reasons to avoid using this deprecated form of command substitution. The preferred command substitution method uses $( ... ) .

Try this instead:

chk_mode=$(sqlplus -s /nolog<<EOF
connect / as sysdba
set pagesize 0 feedback off heading off
select open_mode from v\$database;
EOF
)
1 Like

Did you consider - if no other expansion needs to be done within the here document - to switch off expansion therein (from man bash ):

?

Scrutinizer:

Thank you so much for your advice. You helped me to understand this.