Home Basic variables in Bash
Post
Cancel

Basic variables in Bash

  • Assigning Variables

    Similar to other languages, you can assign variables with the equal notation.

    1
    
      var1='Moon;
    

    Then reference with $ notation.

    1
    
      echo $var
    
    1
    2
    
      Result: 
      Moon
    
  • Assigning String Variables

    Name your variable as you like (something sensible!):

    1
    2
    3
    
          firstname='Cynthia'
          lastname='Liu'
          echo 'Hi there' $firstname $lastname
    
    1
    2
    
      Result:
      Hi there Cynthia Liu
    
  • Missing the $ notation

    If you miss the $ notation - it isn’t a variable

    1
    2
    3
    
      firstname='Cynthia'
      lastname='Liu'
      echo 'Hi there ' firstname lastname
    
    1
    2
    
      Result:
      Hi there firstname lastname
    
  • (Not) assigning variables

    Bash is not very forgiving about spaces in variable creation. Beware of adding spaces!

    1
    2
    
      var1 = "Moon"
      echo $var1
    

    script.sh: line 3: var1: command not found

    Don’t add spaces before and after the = sign.

  • Single, double, backticks

    In Bash, using different quotation marks can mean different things. Both when creating variables and printing.

    • Single quotes ('sometext')=Shell interprets what is between literally
    • Double quotes ("sometext")=Shell interprets literally except using $ and backticks The last way creates a ‘shell-within-a-shell’, outlined below. Useful for calling command-line programs. This is done with backticks.
    • Backticks(``) = Shell runs the command and captures STDOUT back into a variable.
  • Different variable creation

    Let’s see the effect of different types of variable creation

    1
    2
    3
    
      now_var='NOW'
      now_var_singlequote='$now_var'
      echo $now_var_singlequote
    

    Returns: $now_var

    1
    2
    
      now_var_doublequote="$now_var"
      echo $now_var_doublequote
    

    Returns: NOW

  • The date program

    The Date program will be useful for demonstrating backticks Normal output of this program:

    1
    
      date
    

    Mon 2 Dec 2019 14:07:10 AEDT

  • Shell within a shell

    Let’s use the shell-within-a-shell now:

    1
    2
    
      rightnow_doublequote="The date is `date`."
      echo $rightnow_doublequote
    

    Returns: The date is Mon 2 Dec 2019 14:13:35 AEDT.

    The date program was called, output captured and combined in-line with the echo call. We used a shell within a shell

  • Parentheses vs backticks

    There is an equivalent to backtick notation:

    1
    2
    3
    4
    
      rightnow_doublequotes="The date is `date`."
      rightnow_parentheses="The date is $(date)."
      echo $rightnow_doublequotes
      echo $rightnow_parentheses
    
    1
    2
    3
    
      Returns:
      The date is Mon 2 Dec 2019 14:54:34 AEDT.
      The date is Mon 2 Dec 2019 14:54:34 AEDT.
    

    Both work the same though using blackticks is older. Parentheses is used more in modern applications.


Numeric variables in Bash

  • Numbers in other languages

    Numbers are not built in natively to the shell like most REPLs (console) such as R and Python

    In Python or R you may do:

    1
    
      >>> 1 + 4
    

    Returns: 5

  • Numbers in the shell

    Numbers are nto natively supported: (In the terminal)

    1
    
      1 + 4
    

    bash: 1: command not found

  • Introducing expr

    expr is a useful utility program (just like cat or grep) This will now work (in the terminal):

    1
    
      expr 1 + 4
    

    Returns: 5

  • expr limitations

    expr cannot natively handle decimal places; (In terminal)

    1
    
      expr 1 + 2.5
    

    expr: not a decimal number: ‘2.5’ We can get past this limiation using bc

  • Getting numbers to bc

    Using bc without opening the calculator is possible by piping:

    1
    
      echo "5 + 7.5" | bc
    

    12.5

  • bc scale argument

    bc also has a scale argument for how many decimal places.

    1
    
      echo "10/3" | bc
    

    3

    1
    
      echo "scale=3; 10/3" | bc
    

    Note the use of ; to separate ‘lines’ in terminal 3.333

  • Numbers in Bash Scripts

    We can assign numeric variables just like string variables:

    1
    2
    3
    
      dog_name='Roger'
      dog_age=6
      echo "My dog's name is $dog_name and he is $dog_age years old"
    

    Beware that dog_age="6" will work, but makes it a string!

    My dog’s name is Roger and he is 6 years old

  • Double bracket notation

    A variant on single bracket variable notation for numeric variables:

    1
    2
    
      expr 5 + 7
      echo $((5+7))
    

    Returns:
    12
    12

    Beawre this method uses expr, not bc

  • Shell within a shell revisited

    Very useful for numeric variables:

    1
    2
    3
    4
    
      model1=87.65
      model2=89.20
      echo "The total score is $(echo "$model1 + $modle2" | bc)"
      echo "The average score is $(echo "($model1 + $modle2) / 2" | bc)"
    

    Returns
    The total score is 176.85
    The average score is 88


Arrays in Bash

  • What is an array?

    Two types of arrays in Bash:

    • An array
      • ‘Normal’ numerical-indexed structure.
      • Called a ‘list’ in Python or ‘vector’ in R. In Python: my_list=[1,3,2,4] In R: my_vector <- c(1,3,2,4)`
This post is licensed under CC BY 4.0 by the author.