Introduction to Bash
Bash (or shell) scripting is a great way to automate repetitive tasks and can save you a ton of time as a developer. Bash scripts execute within a Bash shell interpreter terminal. Bash is a Unix shell, which is a command line interface (CLI) for interacting with an operating system (OS). Any command that you can run from the command line can be used in a bash script. Scripts are used to run a series of commands. It is available by default on Linux and macOS operating systems.
Variables
In Linux shell scripting two types of variables are used:
System Defined Variables & User Defined Variables.
👉 A variable in a shell script is a means of referencing a numeric or character value. And unlike formal programming languages, a shell script doesn’t require you to declare a type for your variables.
To Print the value of above variables, use echo
command as shown below :
# echo $HOME
# echo $USERNAME
Within bash scripts (or the terminal for that matter), variables are declared by setting the variable name equal to another value.
We can tap into these environment variables from within your scripts by using the environment variable’s name preceded by a dollar sign. This is demonstrated in the following script:
$ cat myscript
#!/bin/bash
# display user information from the system.
echo “User info for userid: $USER”
echo UID: $UID
echo HOME: $HOME
Notice that the environment variables in the echo commands are replaced by their current values when the script is run. Also notice that we were able to place the $USER system variable within the double quotation marks in the first string, and the shell script was still able to figure out what we meant. There is a drawback to using this method, however. Look at what happens in this example:
$ echo “The cost of the item is $15”
The cost of the item is 5
Whenever the script sees a dollar sign within quotes, it assumes you’re referencing a variable. In this example the script attempted to display the variable $1 (which was not defined), and then the number 5. To display an actual dollar sign, you must precede it with a backslash character:
$ echo “The cost of the item is \$15”
The cost of the item is $15
👉 The backslash allowed the shell script to interpret the dollar sign as an actual dollar sign, and not a variable.
Strings
conditionals
When bash scripting, you can use conditionals to control which set of commands within the script run. Use if to start the conditional, followed by the condition in square brackets ([ ]).
Loops
There are 3 different ways to loop within a bash script: for, while and until. A for loop is used to iterate through a list and execute an action at each step.
Arrays
Bash provided one-dimensional array variables. Any variable can be used as an array. the declare built-in will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirements that members be indexed or assigned contiguously. Arrays are zero-based.
Comparison
User Inputs
To make bash scripts more useful, we need to be able to access data external to the bash script file itself. The first way to do this is by prompting the user for input.
Aliases - what are they?
👉 Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias commands.
👉 Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias.
🛑 Alias are expended when the function definition is read, not when the function is executed, because a function definition itself is a compound command. As a consequence, aliases defined in a function are not available until after that function is executed. **
You can set up aliases for your bash scripts within your .bashrc or .bash_profile
file to allow calling your scripts without the full filename.
Shell built-in commands
Built-in commands are contained within the shell itself. When the name of a built in command is used as the first word of a simple command, the shell executes the command directly, without creating a new process. Built-in commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.
3 types of built-in commands are supported by Bash:
- Bourne Shell built-ins:
break
, cd
, continue
, eval
, exec
, exit
, export
, getopts
, hash
, pwd
, readonly
, return
, set
, shift
, test
, times
, trap
, unmask
and unset
- Bash built-in commands:
alias
, bind
, builtin
, command
, declare
, echo
, enable
, help
, let
, logout
, local
, printf
, read
, type
, shopt
, typeset
, ulimit
, unalias
,
Shell execution - Executing programs from a script
When a program being executed is a shell script, bash will create a new bash precess using a fork.
$ echo "Hello, $(whoami)!"
How to create a bash built script
With the command touch
you can create a file.
$ touch hello-everybody