Podcast
Questions and Answers
What command is used to display environmental variables in the shell?
What command is used to display environmental variables in the shell?
- echo
- env (correct)
- set
- printenv
The variable assignment in shell requires spaces before and after the equals sign.
The variable assignment in shell requires spaces before and after the equals sign.
False (B)
What will the command 'echo $name' return if 'name' is assigned as 'John Doe'?
What will the command 'echo $name' return if 'name' is assigned as 'John Doe'?
John Doe
To assign a variable, you use the syntax variable1=_______.
To assign a variable, you use the syntax variable1=_______.
Match the shell command with its expected output:
Match the shell command with its expected output:
What will be the output of the command 'echo $name' if 'name' is set to 'John*'?
What will be the output of the command 'echo $name' if 'name' is set to 'John*'?
The command 'name=John' followed by 'name=John Doe' will successfully change the value of 'name'.
The command 'name=John' followed by 'name=John Doe' will successfully change the value of 'name'.
In the context of shell variables, what does 'TMOUT=0' signify?
In the context of shell variables, what does 'TMOUT=0' signify?
What is displayed by the variable $0 in shell scripts?
What is displayed by the variable $0 in shell scripts?
The variable $@ represents the same information as $*.
The variable $@ represents the same information as $*.
What command is used to remove the first argument from the list in a shell script?
What command is used to remove the first argument from the list in a shell script?
In the command cmdargs_demo a b c
, the total number of command line arguments passed is: ____.
In the command cmdargs_demo a b c
, the total number of command line arguments passed is: ____.
Match the shell script variables with their descriptions:
Match the shell script variables with their descriptions:
The command shift 3
removes the first three arguments from the list.
The command shift 3
removes the first three arguments from the list.
How many positional parameters can be accessed directly in a shell script?
How many positional parameters can be accessed directly in a shell script?
What is a shell script primarily composed of?
What is a shell script primarily composed of?
A shell variable can only be read but not written to.
A shell variable can only be read but not written to.
What command would you use to make a script file executable?
What command would you use to make a script file executable?
The command used to run a shell script in the Bourne Shell is $ ____ script_file.
The command used to run a shell script in the Bourne Shell is $ ____ script_file.
Match the shell variable to its corresponding purpose:
Match the shell variable to its corresponding purpose:
Which command is used for command substitution in BASH?
Which command is used for command substitution in BASH?
Command line arguments can be passed to shell programs.
Command line arguments can be passed to shell programs.
What is the significance of the shebang (#!) in a shell script?
What is the significance of the shebang (#!) in a shell script?
Which of the following elements should be included in a program header for a shell script?
Which of the following elements should be included in a program header for a shell script?
The three basic types of statements for controlling flow in a script are two-way branching, loop statements, and multiway branching.
The three basic types of statements for controlling flow in a script are two-way branching, loop statements, and multiway branching.
What is the purpose of using comments in a script?
What is the purpose of using comments in a script?
A ________ statement in programming allows a script to execute commands repeatedly.
A ________ statement in programming allows a script to execute commands repeatedly.
Match the following programming concepts with their descriptions:
Match the following programming concepts with their descriptions:
In the provided example script, what is the 'secretcode' variable set to?
In the provided example script, what is the 'secretcode' variable set to?
The 'while' statement continues executing as long as its condition is true.
The 'while' statement continues executing as long as its condition is true.
What is a program header?
What is a program header?
What is the purpose of command substitution in a shell?
What is the purpose of command substitution in a shell?
The 'readonly' command allows for the assignment of new values to a variable after it has been defined.
The 'readonly' command allows for the assignment of new values to a variable after it has been defined.
What command is used to read one line from standard input and assign it to variables?
What command is used to read one line from standard input and assign it to variables?
The command used to remove a variable or function is called ______.
The command used to remove a variable or function is called ______.
Match the following commands with their purposes:
Match the following commands with their purposes:
What does the 'shift[N]' command do in a shell script?
What does the 'shift[N]' command do in a shell script?
The 'set' command can only be used with arguments; it has no functionality without them.
The 'set' command can only be used with arguments; it has no functionality without them.
What is the purpose of the 'export' command in a shell?
What is the purpose of the 'export' command in a shell?
Which command is used to add execute permission to a script file?
Which command is used to add execute permission to a script file?
Shell programs are typically stored in ordinary UNIX files.
Shell programs are typically stored in ordinary UNIX files.
What is the purpose of the shebang (#!) at the beginning of a shell script?
What is the purpose of the shebang (#!) at the beginning of a shell script?
The command to execute a shell script directly is $ ________ script_file.
The command to execute a shell script directly is $ ________ script_file.
Match the following shell variable names with their descriptions:
Match the following shell variable names with their descriptions:
What is a shell variable primarily used for?
What is a shell variable primarily used for?
What is it called when command line arguments are passed to a shell script?
What is it called when command line arguments are passed to a shell script?
What will happen if a variable is defined as 'name=John*'?
What will happen if a variable is defined as 'name=John*'?
The command 'name='John Doe'' is valid for assigning a variable in the shell.
The command 'name='John Doe'' is valid for assigning a variable in the shell.
What is indicated by the variable 'TMOUT=0'?
What is indicated by the variable 'TMOUT=0'?
The command to display all environmental variables is '______'.
The command to display all environmental variables is '______'.
Match the following environmental variables with their descriptions:
Match the following environmental variables with their descriptions:
Which of the following is a correct method for defining a shell variable with spaces?
Which of the following is a correct method for defining a shell variable with spaces?
Environmental variables can only be read and not modified.
Environmental variables can only be read and not modified.
What is the output of 'echo $name' if 'name' was set to 'John Doe'?
What is the output of 'echo $name' if 'name' was set to 'John Doe'?
What is the purpose of the readonly
command in the shell?
What is the purpose of the readonly
command in the shell?
The command unset [name-list]
can be used to add new variables to the shell.
The command unset [name-list]
can be used to add new variables to the shell.
What does the command shift[N]
achieve in a shell script?
What does the command shift[N]
achieve in a shell script?
The export
command is used to make variables available to ________ executed from the shell.
The export
command is used to make variables available to ________ executed from the shell.
Match the following shell commands with their purposes:
Match the following shell commands with their purposes:
What is the primary function of command substitution?
What is the primary function of command substitution?
The set
command, when executed without any arguments, displays the names of all shell variables and their current values.
The set
command, when executed without any arguments, displays the names of all shell variables and their current values.
How can you remove a variable or function defined in the shell?
How can you remove a variable or function defined in the shell?
What is displayed by the variable $0 within a shell script?
What is displayed by the variable $0 within a shell script?
The $* variable displays the same values as the $@ variable when passing arguments to a shell script.
The $* variable displays the same values as the $@ variable when passing arguments to a shell script.
How many command line arguments can you access directly using positional parameters in a shell script?
How many command line arguments can you access directly using positional parameters in a shell script?
The command line argument that references all the parameters passed is represented by _______.
The command line argument that references all the parameters passed is represented by _______.
Match the shell script command with its description:
Match the shell script command with its description:
What happens to the arguments after executing 'shift' in a shell script?
What happens to the arguments after executing 'shift' in a shell script?
You can use the command 'shift 3' to remove the first three arguments from the list in a shell script.
You can use the command 'shift 3' to remove the first three arguments from the list in a shell script.
What command would you use to print the second command line argument in a shell script?
What command would you use to print the second command line argument in a shell script?
What is NOT included in a program header for a shell script?
What is NOT included in a program header for a shell script?
Comments in a shell script are optional and can be omitted without impact.
Comments in a shell script are optional and can be omitted without impact.
What type of statement allows commands to execute repeatedly in a shell script?
What type of statement allows commands to execute repeatedly in a shell script?
The three basic types of statements for controlling flow in a script are: two-way branching, multiway branching, and ________.
The three basic types of statements for controlling flow in a script are: two-way branching, multiway branching, and ________.
Match the following elements with their purposes in a program header:
Match the following elements with their purposes in a program header:
Which of the following best describes 'two-way branching'?
Which of the following best describes 'two-way branching'?
The while statement in a shell script continues executing as long as its condition is false.
The while statement in a shell script continues executing as long as its condition is false.
What is the purpose of putting comments in a shell script?
What is the purpose of putting comments in a shell script?
Flashcards
Shell Script
Shell Script
A shell program containing shell commands to be executed by a shell, stored in a file.
Shell Variable
Shell Variable
A storage location used by users and programmers to hold data temporarily.
Command Substitution
Command Substitution
A technique where the output of one command is used as input to another.
Executable Script
Executable Script
Signup and view all the flashcards
Running a Shell Script
Running a Shell Script
Signup and view all the flashcards
Read-only Variable
Read-only Variable
Signup and view all the flashcards
Control Flow Commands
Control Flow Commands
Signup and view all the flashcards
Command-line arguments
Command-line arguments
Signup and view all the flashcards
Shell Variable Assignment
Shell Variable Assignment
Signup and view all the flashcards
Shell Variable Value Retrieval
Shell Variable Value Retrieval
Signup and view all the flashcards
Quoting Variables (Double Quotes)
Quoting Variables (Double Quotes)
Signup and view all the flashcards
Quoting Variables (Single Quotes)
Quoting Variables (Single Quotes)
Signup and view all the flashcards
Dollar Sign for Variables
Dollar Sign for Variables
Signup and view all the flashcards
Shell Variable Names
Shell Variable Names
Signup and view all the flashcards
Quoting Special Characters
Quoting Special Characters
Signup and view all the flashcards
Shell Environment Variables
Shell Environment Variables
Signup and view all the flashcards
Shell Script Header
Shell Script Header
Signup and view all the flashcards
Two-way Branching
Two-way Branching
Signup and view all the flashcards
Multiway Branching
Multiway Branching
Signup and view all the flashcards
Repetitive Execution
Repetitive Execution
Signup and view all the flashcards
The test
Command
The test
Command
Signup and view all the flashcards
The for
Statement
The for
Statement
Signup and view all the flashcards
The while
Statement
The while
Statement
Signup and view all the flashcards
Program Control Flow Commands
Program Control Flow Commands
Signup and view all the flashcards
Back Quotes
Back Quotes
Signup and view all the flashcards
Exporting Variables
Exporting Variables
Signup and view all the flashcards
Resetting Variables
Resetting Variables
Signup and view all the flashcards
Reading From Standard Input
Reading From Standard Input
Signup and view all the flashcards
Shifting Arguments
Shifting Arguments
Signup and view all the flashcards
Setting Arguments
Setting Arguments
Signup and view all the flashcards
Special echo
Characters
Special echo
Characters
Signup and view all the flashcards
What is the purpose of $0
in a shell script?
What is the purpose of $0
in a shell script?
Signup and view all the flashcards
How do you access the number of command-line arguments?
How do you access the number of command-line arguments?
Signup and view all the flashcards
What is the purpose of $@
and $*
?
What is the purpose of $@
and $*
?
Signup and view all the flashcards
What does the shift
command do?
What does the shift
command do?
Signup and view all the flashcards
How does shift 3
impact arguments?
How does shift 3
impact arguments?
Signup and view all the flashcards
What is the purpose of $1
, $2
, $3
, etc.?
What is the purpose of $1
, $2
, $3
, etc.?
Signup and view all the flashcards
What is the purpose of exit 0
?
What is the purpose of exit 0
?
Signup and view all the flashcards
What is the purpose of exit 1
?
What is the purpose of exit 1
?
Signup and view all the flashcards
How to make a script executable?
How to make a script executable?
Signup and view all the flashcards
Read-only Shell Variable
Read-only Shell Variable
Signup and view all the flashcards
Passing Arguments to a Script
Passing Arguments to a Script
Signup and view all the flashcards
Displaying Shell Variables
Displaying Shell Variables
Signup and view all the flashcards
Shell Variable Retrieval
Shell Variable Retrieval
Signup and view all the flashcards
Double Quotes for Variables
Double Quotes for Variables
Signup and view all the flashcards
Single Quotes for Variables
Single Quotes for Variables
Signup and view all the flashcards
What does env
do?
What does env
do?
Signup and view all the flashcards
What is the purpose of $0
?
What is the purpose of $0
?
Signup and view all the flashcards
What does exit 0
do?
What does exit 0
do?
Signup and view all the flashcards
What does exit 1
do?
What does exit 1
do?
Signup and view all the flashcards
Program Header
Program Header
Signup and view all the flashcards
What are command-line arguments?
What are command-line arguments?
Signup and view all the flashcards
How do you access the first argument?
How do you access the first argument?
Signup and view all the flashcards
What is $0
?
What is $0
?
Signup and view all the flashcards
What is $#
?
What is $#
?
Signup and view all the flashcards
What is shift
command?
What is shift
command?
Signup and view all the flashcards
How does shift
affect arguments?
How does shift
affect arguments?
Signup and view all the flashcards
Exporting Environment Variables
Exporting Environment Variables
Signup and view all the flashcards
Special Characters in echo
Special Characters in echo
Signup and view all the flashcards
Study Notes
Introduction to BASH Scripting
- Shell scripting introduces the concept of shell programming.
- Shell programs are executed by a shell.
- Shell variables are used for reading and writing storage.
- Command line arguments are passed to shell programs.
- Command substitution is a concept.
- Basic coding principles are described.
- Shell scripts are written and discussed.
- Commands and primitives are covered.
Shell Script
- A shell script is a shell program.
- Shell commands are executed by the shell.
- This script is stored in a UNIX file.
- Shell variables provide read/write storage for users and programmers.
- They are scratch pads for tasks.
- Program control flow commands define sequential order of execution.
Running a Bourne Shell Script
- Make the script file executable by adding execute permission.
- Example command:
chmod u+x script_file
- Run the script using the
bash
command. - Example command:
/bin/bash script_file
- Force the current shell to execute the script in the Bourne Again Shell.
- Example command:
#!/bin/bash
Shell Variables
- CDPATH contains directory names searched by
cd
. - EDITOR is the default editor for programs like email.
- ENV contains paths to configuration files.
- HOME describes the user's home directory.
- MAIL is the name of the user's mailbox file.
- MAILCHECK specifies interval for checking the mailbox.
- PATH contains directories for finding external commands.
- PPID is the parent process ID.
- PS1 is the primary prompt, usually
$
. - PS2 is the secondary prompt, typically
>
. - PWD represents the current working directory.
- TERM is the terminal type.
Read-only Shell Variables
$0
: Program name.$1-$9
: Command line arguments (1-9).$*
: All command line arguments (quoted).$@
: All command line arguments (individually quoted).$#
: Total arguments.$$
: Process ID (PID).$?
: Exit status of last command.$!
: PID of last background process.
Display Shell Variables
- Shell variables are shown by using
set
. - Variables and their respective values are displayed.
Reading and Writing Shell Variables
- Assign values to variables using
variable1=value1[variable2=value2...variableN=valueN]
. - No spaces before or after the equals signs.
Command Substitution
- Enclose a command in back quotes to execute and substitute with the output.
- Example:
echo "$(date)"
Exporting Environment
- Use
export
to copy variable values to other commands. export name
copies the value ofname
to other commands.- Example:
name="John Doe"; export name
.
Resetting Variables
- Use
unset
to reset variables. - Remove the variable or function corresponding to the names in the list.
- Example command:
unset name place
Creating Read-Only Defined Variables
- Use
readonly
to prevent assignment of new values to variables. - This prevents modification.
- Example use:
readonly name place
Reading from Standard Input
- Use
read variable-list
to read a line from standard input. - Assign words in the line to variables.
Passing Arguments to Shell Scripts
shift [N]
: Shifts command line arguments N positions to the left.set [options] [argument-list]
: Sets values of positional arguments. (set
alone displays current variable values).
Special Characters for the echo Command
- Special characters to use within the
echo
command, including:\b
,\c
,\f
,\n
,\r
,\t
,\v
,\\
,\ON
.
Passing Arguments to Shell Scripts: Examples
- Examples demonstrate how to pass arguments to shell scripts.
Comments and Program Headers
- Use comments to explain sections of code.
- Include program headers for every script, including file name, author, date written, date modified, purpose and algorithm.
Program Control Flow Commands
- Used to control the order of program statements.
- Examples: branching, loops.
The if Statement (Two-way branching)
- Conditional statements based on an expression.
- Implement conditional logic in scripts.
- Syntax:
if
expressionthen
commands
Operators for the test Command
- Operators for the
test
command (or[ ]
), including file testing (e.g.,-d file
,-f file
,-r file
,-s file
,-t [filedes]
,-w file
,-x file
) and integer/string testing.
The for Statement
for variable [in argument-list] do command-list done
: Loops through items in a list.
The while Statement
- Repeats commands as long as an expression is true.
The until Statement
- Repeats commands while an expression is false.
The break and continue Statements
break
: Exits the loop immediately.continue
: Skips the rest of the current iteration.
The case Statement
- Used for multi-way branching.
- Example use:
case "$option" in d|D) date;; 1|L) ls;; esac.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge with this quiz on shell scripting essentials. From environmental variables to command-line arguments, this quiz covers key concepts and commands you'll encounter while working in the shell. Perfect for beginners and those looking to refresh their skills!