Skip to content

Instantly share code, notes, and snippets.

@jainxy
Last active February 21, 2020 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jainxy/c76fbc64712225b04ea0dd3138892ccd to your computer and use it in GitHub Desktop.
Save jainxy/c76fbc64712225b04ea0dd3138892ccd to your computer and use it in GitHub Desktop.
Bash shell scripting

References

Notes

Introduction

  • Bash (AKA Bourne Again Shell) is a type of interpreter that processes shell commands.
  • A shell interpreter takes commands in plain text format and calls Operating System services to do something.
  • Bash is the improved version of Sh (Bourne Shell).
  • A shell scripting is writing a program for the shell to execute and a shell script is a file or program that shell will execute.
  • Terminal is the interface to the shell interpreter.
  • A shell script is a fully-fledged programming language in itself. It can define variables, functions and we can do conditional execution of shell commands as well.
  • When we enter a command like ls or touch, the shell interpreter first checks for an alias present in .bash_profile file in wer home directory. If alias is not present, then it looks for a binary executable file in the PATH.
  • Normally, a Bash script file has .sh extension to make it clear that it is a shell script file.
  • we can also directly execute it like a binary but we need to put a shebang or hashbang line at the top of the file to declare the interpreter.
  • #! /usr/bin/env bash
  • When we run a script file in the terminal, terminal issues a new session for the script to run.

Declare Variables

  • To declare a variable and assign with a value, use VARIABLE_NAME=VALUE expression (with no spaces in between)
  • Bash does not have a type system, it can only save string values. Hence internally, Bash saves them as a string.
  • However, based on operations, Bash might convert them to a suitable data type on the fly.
  • echo adds a space automatically between two adjacent arguments (in the output) by default.
  • read user input in the terminal using 'read' command. When the user input the text and hit enter, that entire text will be saved in a variable.
  • echo command with -n flag, will prevent adding a newline to the terminal so that user can input text on the same line.
  • read command blocks the script execution until the user presses enter in the terminal.

Save command output to variable

  • VARIABLE_NAME=$(cmd) or VARIABLE_NAME=<backtick>cmd<backtick>
  • The newline in Bash is a command separator but we can also use ;. If we want to write multiple commands on a single line, then use ; as the command separator. For example, echo $PWD; echo $BASH_VERSION.

String Interpolation

  • We can concatenate two variable by just placing them side by side.
  • FULL_NAME=$FIRST_NAME$LAST_NAME; echo $FULL_NAME
  • Bash also supports += operator to concatenate two strings.
  • NUMBER=1; NUMBER+=2; NUMBER+=$NUMBER; echo $NUMBER
  • We can also substitute a variable in a string defined by double-quotes by directly putting it in with $ prefix as we would use outside or with ${VARIABLE_NAME}
  • FIRST_NAME='JOHN' ; LAST_NAME='DOE' ; echo Hello ${FULL_NAME}!
  • Bash supports both single-quotes (‘) and double-quotes (“) to define a string. But only double quotes are capable of string interpolation.
  • But if we need to put single or double quote as a character in a string, we need to escape it with a backslash character.
  • FIRST_NAME='JOHN' ; echo "Hello ${FIRST_NAME}!" ; echo 'Hello ${FIRST_NAME}!'
  • When we reference a string variable and pass it as an argument to a command like echo, Bash breaks it into different words (separated by a space) and pass them as individual arguments.
  • args command prints the number of arguments passed to it.
  • Use string interpolation by wraping a variable in double-quotes.

Arithmetic commands

  • We can perform arithmetic operations in Bash even though Bash does not support number data type.

let command

  • let RESULT=1+1; echo $RESULT
  • let would evaluate the expression in a string. So if we want to use interpolation to generate a string, it’s possible.
  • We can use any arithmetic operation like +, -, * or /. +=, -= , *=, /= and %=
  • We can also calculate the modulus using % operator. We can also increment or decrement a variable using VAR++ , ++VAR, VAR-- or --VAR expression.
  • NUMBER=1; let RESULT="++NUMBER"; echo $RESULT; echo $NUMBER
  • We can also declare a variable inside the expression using let or perform an arithmetic operation. Using this, we can also add spaces in the expression.

expr command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment