Skip to content

Shell Scripting Basics

Nuno Nogueira edited this page Dec 14, 2022 · 16 revisions

Using Multiple Commands

  • Use multiple command separate by ;. Example:
    my_var="Good Cat";echo $my_var
    

Command Substitution

  • Extract information from the output of a command and assign it to a variable with the:

    • backtick/backquote - ` - character -> number 96 in ASCII and code U+0060 in Unicode. Example:
    date_var=`date`
    
    • $() format. Example:
    date_var=$(date)
    

Redirecting Input and Output

Output redirection

  • To send the output from a command to a file we use command > outputfile. Examples:

    same Shell directory different Shell directory
    date > test_file_date.txt date > /DIRECTORY_YOU_WANT/test_file_date.txt

Input Redirection

  • To take the content of a file and redirects it to a command we use command < inputfile. Examples using the wc command:

    same Shell directory different Shell directory
    wc < test_file_date.txt wc < /DIRECTORY_YOU_WANT/test_file_date.txt

Inline Input Redirection

  • To specify the data for Input Redirection on the command line instead of in a file we use command << marker. Example:

    Shell Input command
    user@hostname $ wc << INPUT_DATA
    heredoc> test 1
    heredoc> test 2
    heredoc> test 3
    heredoc> INPUT_DATA
    3 6 21

Pipes

  • To send the output of one command to the input of another command we use command1 | command2. It is like a Output Redirection command > outputfile then a Input Redirection command < inputfile without the need for a file. Example:
    fc-list : family | sort
    

    The command fc-list list the available fonts and font styles installed in the system.


  • There is no limit to the number of the Pipes we can use. Examples:
    fc-list : family | sort | more
    
    fc-list : family | sort > FONT_LIST_SORTED.txt
    

Performing Math

expr Command

  • The expr Command allowed the processing of equations from the command line. Example:
    expr 4 + 2
    

    The expr --help Command show the expr Command help in the Shell


  • expr Command Operators:

    Operator Description
    ARG1 | ARG2 Returns ARG1 if neither argument is null or zero; otherwise, returns ARG2
    ARG1 & ARG2 Returns ARG1 if neither argument is null or zero; otherwise, returns 0
    ARG1 < ARG2 Returns 1 if ARG1 is less than ARG2; otherwise, returns 0
    ARG1 <= ARG2 Returns 1 if ARG1 is less than or equal to ARG2; otherwise, returns 0
    ARG1 = ARG2 Returns 1 if ARG1 is equal to ARG2; otherwise, returns 0
    ARG1 != ARG2 Returns 1 if ARG1 is not equal to ARG2; otherwise, returns 0
    ARG1 >= ARG2 Returns 1 if ARG1 is greater than or equal to ARG2; otherwise, returns 0
    ARG1 > ARG2 Returns 1 if ARG1 is greater than ARG2; otherwise, returns 0
    ARG1 + ARG2 Returns the arithmetic sum of ARG1 and ARG2
    ARG1 - ARG2 Returns the arithmetic difference of ARG1 and ARG2
    ARG1 * ARG2 Returns the arithmetic product of ARG1 and ARG2
    ARG1 / ARG2 Returns the arithmetic quotient of ARG1 divided by ARG2
    ARG1 % ARG2 Returns the arithmetic remainder of ARG1 divided by ARG2
    STRING : REGEXP Returns the pattern match if REGEXP matches a pattern in STRING
    match STRING REGEXP same as STRING : REGEXP
    substr STRING POS LENGTH Returns the substring LENGTH characters in length, starting at position POS (starting at 1)
    index STRING CHARS Returns position in STRING where CHARS is found; otherwise, returns 0
    length STRING Returns the numeric length of the string STRING
    + TOKEN Interprets TOKEN as a string, even if it’s a keyword
    (EXPRESSION) Returns the value of EXPRESSION
  • IMPORTANT NOTE: Standard operators work fine in the expr Command but problems can occur when we use them from a script or the command line. Many of the expr Command Operators have other meanings in the Shell (such as the * - asterisk ) and to solve this problem, we need to use the Shell Escape Character (the \ - backslash) to identify any characters that may be misinterpreted by the Shell. Example: expr 3 \* 4


Using brackets

  • In Bash, when assigning a mathematical value to a variable, you can enclose the mathematical equation using a dollar sign and square brackets $[OPERATION]. Examples:
    var_1=$[3+4]
    
    var_2=$[$var_1*2]
    

  • Using brackets makes Shell math much easier than with the expr Command. Example:
    var_1=2
    var_2=3
    var_3=4
    var_4=$[$var_1*($var_2+$var_3)]
    

  • IMPORTANT NOTES:
    • When using the square brackets method for calculating equations, we don’t need to worry about the multiplication symbol, or any other characters, being misinterpreted by the Shell. The Shell knows that it’s not a wildcard character because it is within the square brackets.
    • The Bash Shell mathematical operators support only integer arithmetic.