5.1 Given a scenario, deploy and execute basic BASH scripts Flashcards
What is the PATH variable used for?
Scripts should be located in one of the directories defined by the $PATH variable
export
The export command can also be used to display all environment variables:
To convert an existing local variable to an environment variable, use the export command:
[student@OCS ~]$ echo $NAME
Sarah
[student@OCS ~]$ export NAME
env
The env command displays environment variables in the current shell. Local variables are not displayed when the env command is executed
set
The set command displays all shell variables and values when executed with no arguments. The output also includes any functions that have been declared within the shell.
The set command can also be used to modify the behavior of the shell
printenv
Like the env command, the printenv command is used to display environment variables; however, the env command has additional features, whereas the printenv command is used solely to display environment variables
echo
The echo command is used to display information. Typically, it is used to display the value of variables
!/bin/bash
The first line of a script should include the path to the interpreter. For BASH shell scripts, this should be the path to the executable bash command. This path can be discovered by executing the following command:
[root@OCS ~]$ which bash
/bin/bash
!/bin/bash
The first line of a script should include the path to the interpreter. For BASH shell scripts, this should be the path to the executable bash command. This path can be discovered by executing the following command:
[root@OCS ~]$ which bash
/bin/bash
What is meant by sourcing scripts?
Most commands are executed as separate processes that have their own environment. The source command allows you to execute a bash script as if the commands within the script were executed directly on the command line
source ./functions.sh
Use the . command to perform the same function as the source command, as shown next:
[root@OCS ~]$ . ./functions.sh
File Blobbing
A file glob (also called a wildcard) is any character provided on the command line that represents a portion of a filename. The following globs are supported:
Glob
Description
*
Matches zero or more characters in a filename.
?
Matches any single character in a filename.
[ ]
Matches a single character in a filename as long as that character is represented within the [ ] characters.
File Blobbing
A file glob (also called a wildcard) is any character provided on the command line that represents a portion of a filename. The following globs are supported:
Glob
Description
*
Matches zero or more characters in a filename.
?
Matches any single character in a filename.
[ ]
Matches a single character in a filename as long as that character is represented within the [ ] characters.
How would you copy all files that end in .txt
[student@OCS ~]$ cp *.txt ~
What command would you use to remove all files that are four characters long?
[student@OCS ~]$ rm ????
What command would you use to view the file type for all files in the current directory that begin with a, b, or c
[student@OCS ~]$ file [abc]*
or
[student@OCS ~]$ file [a-c]*
What does this shell expansion do: ${}
With the variable expansion BASH feature, you can return a different value. For example, the following will return “Bob” if the $name variable isn’t set:
$ echo “Hello, ${name:-Bob}”
Hello, Bob$
echo “Hello, $name”
Hello,