Score:0

Bash script to get the variable expansion within a section of code that requires single quotes around it

ch flag

I have a command (snmptrap) that requires input strings to be in single quotes. Within a bash script, run this command which I need to insert a variable and expand it. I have tried numerous variation but to no avail. I have also search several web sites to find a similar situation, but no help.

DT=`date '+%a %b %d, %Y at %H:%M:%S'`

Dev="1#.##.##.147"  

DESTINATION=$Dev

VAR3="DATA_TRANSFER_NAVISTOR - EBR -test "$DT" - ERRORLEVEL 111."

/usr/bin/snmptrap -v 1 -c public $DESTINATION 1.3.6.1.4.1.791 10.36.1.177 6 200 '' \
.1.3.6.1.4.1.791.200.1  s 'NS_Monitor' \
.1.3.6.1.4.1.791.200.2  s 'NAVISTOR' \
.1.3.6.1.4.1.791.200.3  s '\"$VAR3\"' \
.1.3.6.1.4.1.791.200.4  s 'CRITICAL' \
.1.3.6.1.4.1.791.200.5  s 'hostname=na900618-sal.delhaize.com'

$VAR3  
$[$VAR3]    
"$VAR3"  
\$VAR3   
\"$VAR3\"
Kevin O. avatar
ch flag
The variable that will not expand is in the line: .1.3.6.1.4.1.791.200.3 s '"$VAR3"'
hr flag
I'm not clear exactly what you mean by *"a section of code that requires single quotes around it"* - the single quotes around command arguments like `snmptrap ... 'NAVISTOR'` are removed by the shell before the `NAVISTOR` argument is passed to the command
NotTheDr01ds avatar
vn flag
There may be a better duplicate for this, but based on your answer it sounds like this *might* answer your original question -- [Difference between $'$foo' and $"$foo"](https://askubuntu.com/questions/736226/difference-between-foo-and-foo)
Score:0
ch flag

A colleague said to simply replace the single quotes with double quotes so that the var3 line is: .1.3.6.1.4.1.791.200.3 s "$VAR3" \

This works.

Artur Meinild avatar
vn flag
You should probably understand the difference between single and double quotes in Bash. Double qoutes expand variables, single qoutes do not.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.