Results 1 to 10 of 12

Thread: BASH script help

Hybrid View

  1. #1
    Join Date
    Oct 2007
    Beans
    333

    BASH script help

    I am trying to grasp some basic BASH scripting, im getting a bit confused regarding variables.

    Code:
    VAR="uptime"
    $VAR
    This would assign the variable as uptime the print the uptime.
    $3 hours, 15 minutes

    Code:
    VAR="uptime"
    echo "Your computer has been on for $VAR"
    I would expect this to output as follows:
    $Your computer has been on for 3 hours, 15 minutes

    However what I get is:
    $Your computer has been on for uptime

    Would I be right in saying $VAR is being read as a "string" and not as a "variable". If this is the case how do I change it to be read as a variable?
    Intel Core 2 Duo 2x 2.33Ghz, FP-IN9 SLI mobo, 2Gb 800Mhz RAM, 256mb GeForce 7300 LE graphics, 120Gb IDE HD, 400Gb IDE HD,

  2. #2
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: BASH script help

    Put it in the varable like so:

    Code:
    VAR=$(uptime)
    The one you have set in the OP will just echo uptime to the terminal, not run the command.

    The only problem is that uptime prints out like this:

    Code:
    charles@atlantis:~$ uptime
     12:42:21 up 1 day,  5:40,  1 user,  load average: 0.00, 0.00, 0.00
    Last edited by CharlesA; October 9th, 2010 at 08:45 PM.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  3. #3
    Join Date
    Oct 2007
    Beans
    333

    Re: BASH script help

    this fixed the problem, thankyou
    Intel Core 2 Duo 2x 2.33Ghz, FP-IN9 SLI mobo, 2Gb 800Mhz RAM, 256mb GeForce 7300 LE graphics, 120Gb IDE HD, 400Gb IDE HD,

  4. #4
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    12,981
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: BASH script help

    Code:
    VAR=`uptime`
    Using the "backtick" operator (to the left of 1 in the upper row of the keyboard) evaluates the expression inside the backticks and passes the result to VAR. An alternative to using the backticks is an expression like this:

    Code:
    VAR=$(uptime)

  5. #5
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: BASH script help

    Quote Originally Posted by SeijiSensei View Post
    Code:
    VAR=$(uptime)
    That is the preferred way, since it's easier to nest than backticks.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  6. #6
    Join Date
    Feb 2007
    Beans
    863

    Re: BASH script help

    Quote Originally Posted by SeijiSensei View Post
    Code:
    VAR=`uptime`
    Using the "backtick" operator (to the left of 1 in the upper row of the keyboard) evaluates the expression inside the backticks and passes the result to VAR. An alternative to using the backticks is an expression like this:

    Code:
    VAR=$(uptime)
    the
    Code:
     VAR=`uptime`
    is usually adviced, because it is more powerful"" and less messy

  7. #7
    Join Date
    Feb 2007
    Beans
    863

    Re: BASH script help

    Question:

    Code:
    VAR=`cat FILE`
    if [ "$VAR" != "" ] ; then

    or

    Code:
    if [ "`cat FILE`" != "" ] ; then

    which one is better/working ?

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: BASH script help

    imo it's the same in this case
    if i am not mistaken `` is less desirable when you want to process the output of a command in multiple steps - `` is like macro so in reality it's the command itself substituted for the variable, thus making it that each time variable is used, the command is run.
    $(...) on the other hand stores the output in a variable, so the command is only run once. I may be mistaken though so take that with a grain of salt.

  9. #9
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: BASH script help

    See the thread here (and links therein) for more info as to why $(...) is prefered to backticks.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •