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?
You can create a local variable in Bash by using the “local” keyword. Take the following code for example:
#!/bin/bash function my_function() { local my_variable="Hello World" echo $my_variable } 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
let "x = 5 + 3"
- The double parentheses syntax “$(())”
x=$((5 + 3))
- The “expr” command
result=$(expr 5 + 3)
How could you find the sum of 4 and 5 using the following three arithmetic methods?
let $(()) 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)
Suppose you have the following array:
addresses=(172.16.1.1 172.16.1.2 172.16.1.3)
How could you use “echo” to quickly print all addresses in the array?
echo “${addresses[*]}”
The asterisk is a representation of every array element.
Suppose you have the following array:
addresses=(172.16.1.1 172.16.1.2 172.16.1.3)
How could you remove just the second element in the array?
unset addresses[1]
What are streams in Bash?
In Bash, “streams” refer to the channels used to transfer data between processes, commands, or files. Streams are central to how input and output are managed in Linux and Unix-like systems.
The three main streams are:
Standard Input (file descriptor: 0)
Standard Output (file descriptor: 1)
Standard Error (file descriptor: 2)
A file descriptor is a reference to an open file or resource maintained by the kernel.
Suppose you want to create two files named ‘test1’ and ‘test2’ but you only want to create the second file if the first file is successfully created. How can you do this in one simple line?
touch test1 && touch test2
If the && operator is found between two commands, the second command will only execute if the first command executes successfully.
Suppose you have three files in your current directory each named ‘conf1’, ‘conf2’ and ‘conf3’ respectively. With one line, how can you search for the text ‘## port:’ in these files but stop the search once you find the first file containing this text?
grep ‘## port:’ conf1 || grep ‘## port:’ conf2 || grep ‘## port:’ conf3
When there are two commands surrounding the || operator, the second command will only execute if the first command fails (exit code greater than 1) to execute successfully.
Suppose you want to list the contents of the root directory and you want to send the standard output to a file named ‘stdout.txt’ while sending the standard error to the ‘stderr.txt’ file. How could you do this in one simple line?
ls -l / 1> stdout.txt 2> stderr.txt
This above example uses the file descripter numbers to redirect command output to the standard output and standard error streams.
Suppose you want to list the contents of the root directory and you want to write (not append) both the standard output and the standard error to the ‘results.txt’ file. How could you do this in one simple line?
ls -l / &> results.txt
The ‘&>’ operator redirects both standard output and standard error.
If the ‘&»’ operator had been used then both the standard output and standard error would have been appended to ‘results.txt’ rather than written to the file.