LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script: how to assign command ouput to a variable without executing it? (https://www.linuxquestions.org/questions/programming-9/bash-script-how-to-assign-command-ouput-to-a-variable-without-executing-it-862354/)

bostonantifan 02-12-2011 11:20 PM

bash script: how to assign command ouput to a variable without executing it?
 
I'm trying to execute the following command within a bash script:
Code:

tac /var/log/system.log | head -1
The script I wrote is:
Code:

#!/bin/bash
$test1="$(tac /var/log/system.log | -1)
echo $test1

When I run the script, this is the output I get:
Code:

Sat Feb 12 23:24:27 EST 2011
/var/log ->peterv@MBP17.local<528>$: but.sh

/Users/peterv/bin/but.sh: line 2: =Feb 12 23:57:54 MBP17 ReportCrash[14447]: Saved crash report for CapsSee[294] version 1.02 (1.01) to /Users/peterv/Library/Logs/DiagnosticReports/CapsSee_2011-02-12-235754_MBP17.crash: No such file or directory


Sat Feb 12 23:24:27 EST

The output in red above is what I'm expecting to be in variable $test1, but it looks as though it's trying to execute that text as a command. I'm assuming that there's some syntax I fouled up, but I can't see it. If anyone can help me out, I'd be very grateful.

bostonantifan 02-12-2011 11:55 PM

Never mind!
 
Never mind! The extraneous "$" I mistakenly prepended to the variable assignment caused my problem.


All times are GMT -5. The time now is 08:54 PM.