Bash Flashcards
How can you quickly verify the version of Bash running on your system?
bash –version
What are Bash environment variables?
When running in a terminal, Bash loads a set of environment variables with every new session that gets invoked. Programs can use these environment variables for various purposes, such as discovering the identity of the user running the script, the location of their home directory, and their default shell.
How can you list the environment variables set by Bash?
- ‘printenv’
- ‘env’ with no additional arguments
The ‘env’ command can also be used to set specific environment variables and then execute a command with those variables.
What are the advantages of this shebang over simply hardcoding the full path to the desired interpreter?
’#!/usr/bin/env python’
Hardcoded shebang:
’#!/usr/bin/python’
Using ‘env’ in the shebang is better because ‘env’ will search for the specified utility (interpreter) with the PATH environment variable.
This means that the script will likely still work across different machines even if Python is installed in different locations.
From the ‘env’ documentation:
“The ‘env’ utility uses the PATH environment variable to locate the requested utility if the name contains no ‘/’ characters, unless the -P option has been specified”
What are some common default environment variables present in Bash?
BASH_VERSION: The bash version running
BASHPID: The process identifier (PID) of the current bash process
GROUPS: A list of groups the running user is a member of
HOSTNAME: The name of the host
OSTYPE: The type of operating system
PWD: The current working directory
RANDOM: A random number from 0 to 32,767
UID: The user ID (UID) of the current user
SHELL: The full pathname to the shell
What shebang makes your Bash scripts more portable?
!/usr/bin/env bash
The shebang line can also take optional arguments to change how the script executes. Examples include:
’#!/bin/bash -x’ prints all commands and their arguments as they are executed to the terminal (useful for debugging)
’#!/bin/bash -r’ creates a restricted bash shell, which restricts certain potentially dangerous commands (navigating to certain directories or modifying sensitive environment variables)
Suppose you have a Bash script named ‘map.sh’ that doesn’t have a shebang line and also doesn’t have an executable permission (+x) set. How can you run this script?
bash map.sh
Suppose you want to run a Bash script in ‘verbose mode’ to let you see the commands being executed for the purpose of debugging. How can you do this?
bash -x
The ‘-x’ option prints commands and their arguments as they are executed
How can you use debugging mode for just a certain section of your script?
Use the ‘set’ command to enable/disable debugging mode (-x) for just a subsection of your script:
set -x
cat /etc/passwd | grep rpcbind
echo $UID
set +x
ls -l
——————————-
‘set’ is used to change/display shell options and positional parameters
In the above script, debugging mode is active for every command except for ‘ls -l’
How can you check the syntax of your Bash script without actually executing the commands?
bash -n
The ‘-n’ option tells Bash to read the commands but not execute them (essentially does a dry run of the script)
What is command substitution in Bash?
Command substitution allows you to use the output of a command as part of another command.
You can also use it to assign command output to a variable.
For command substitution, place your command(s) within the following characters:
$()
Is it valid syntax to assign a variable with spaces around the equals symbol?
No. This is valid variable assignment syntax:
motd=“Welcome to the company network.”
How can you unassign a variable that has already been assigned?
Assume the variable “salt” has already been assigned.
‘unset salt’
The unset command in Bash is used to remove variables or functions from the current shell environment.
How do you create a local variable in Bash?
!/bin/bash
function my_function() {
local my_variable=”Hello World”
echo $my_variable
}
my_function # Prints “Hello World”
echo $my_variable # Prints nothing, as the variable is local to the function
What are three common different methods of performing arithmetic operations in Bash?
- The “let” command
- The double parentheses syntax “$(())”
- The “expr” command
How could you find the sum of 4 and 5 using the following three arithmetic methods?
let
$())) syntax
expr
let result=“4 + 5”
result=$((4 + 5))
result=$(expr 4 + 5)
Suppose you want to create an array named “addresses” that contains the following three IP addresses:
172.16.1.1
172.16.1.2
172.16.1.3
How could you do this?
addresses=(172.16.1.1 172.16.1.2 172.16.1.3)