Podcast
Questions and Answers
How is a shell variable assigned a value?
How is a shell variable assigned a value?
What is the effect of using quotes around variable expansions?
What is the effect of using quotes around variable expansions?
What happens if an undefined variable is expanded without the nounset option?
What happens if an undefined variable is expanded without the nounset option?
Which statement about local and environment variables is correct?
Which statement about local and environment variables is correct?
Signup and view all the answers
Which of the following represents a best practice for variable naming in shell scripts?
Which of the following represents a best practice for variable naming in shell scripts?
Signup and view all the answers
What does the shell do if a command name is not recognized as a built-in command?
What does the shell do if a command name is not recognized as a built-in command?
Signup and view all the answers
How are environment variables formatted in the PATH variable?
How are environment variables formatted in the PATH variable?
Signup and view all the answers
How does the shell determine which command to execute when multiple matches are found in the PATH?
How does the shell determine which command to execute when multiple matches are found in the PATH?
Signup and view all the answers
What happens when a command is not found in any of the directories listed in PATH?
What happens when a command is not found in any of the directories listed in PATH?
Signup and view all the answers
Why should the current directory (.) not be included in PATH?
Why should the current directory (.) not be included in PATH?
Signup and view all the answers
What is the primary purpose of the 'which' command?
What is the primary purpose of the 'which' command?
Signup and view all the answers
How should PATH be modified in a shell script to ensure commands are executed correctly?
How should PATH be modified in a shell script to ensure commands are executed correctly?
Signup and view all the answers
What is a key best practice regarding the use of PATH?
What is a key best practice regarding the use of PATH?
Signup and view all the answers
What should you do to execute a command from the current directory safely?
What should you do to execute a command from the current directory safely?
Signup and view all the answers
What is the primary difference between interactive and scripted use of shells?
What is the primary difference between interactive and scripted use of shells?
Signup and view all the answers
What is the purpose of the shebang (#!) in a script?
What is the purpose of the shebang (#!) in a script?
Signup and view all the answers
Which command is used to make a script executable?
Which command is used to make a script executable?
Signup and view all the answers
What happens when a script is executed in a shell process?
What happens when a script is executed in a shell process?
Signup and view all the answers
How can you handle multiple command-line arguments safely within a script?
How can you handle multiple command-line arguments safely within a script?
Signup and view all the answers
Which method can be used for redirecting output in a script?
Which method can be used for redirecting output in a script?
Signup and view all the answers
Which of the following is a best practice for maintaining script security?
Which of the following is a best practice for maintaining script security?
Signup and view all the answers
What syntax is used for shell command substitution?
What syntax is used for shell command substitution?
Signup and view all the answers
What will the command echo $(( 5 + 3 * 4 )) output?
What will the command echo $(( 5 + 3 * 4 )) output?
Signup and view all the answers
What is the exit status of a successfully executed command in Unix/Linux?
What is the exit status of a successfully executed command in Unix/Linux?
Signup and view all the answers
How can you explicitly set the exit status of a shell script?
How can you explicitly set the exit status of a shell script?
Signup and view all the answers
Which tool is used for decimal point arithmetic in shell scripts?
Which tool is used for decimal point arithmetic in shell scripts?
Signup and view all the answers
What will the command echo '22/7' | bc -l output?
What will the command echo '22/7' | bc -l output?
Signup and view all the answers
When a script finishes execution without any commands, what happens to its exit status?
When a script finishes execution without any commands, what happens to its exit status?
Signup and view all the answers
Which operation is NOT supported in shell arithmetic?
Which operation is NOT supported in shell arithmetic?
Signup and view all the answers
What command retrieves the exit status of the last executed command?
What command retrieves the exit status of the last executed command?
Signup and view all the answers
Study Notes
Shell Variables
- Variables store text or numeric values in shell scripts.
- Assign values using
=
without spaces around it. - Use
\$
to expand variables in commands or scripts. - Single quotes or backslashes prevent immediate variable expansion.
- Always double-quote variables to avoid word-splitting and GLOB expansion.
- Undefined variables expand to nothing unless the
nounset
option is set. - Append to a variable with the syntax:
\$VARNAME=\$VARNAME:newtext
. - Local variables are specific to the current shell; exported variables are inherited by child processes.
- Common pre-set environment variables include
\$PATH
,\$USER
,\$HOME
,\$SHELL
,\$TERM
, and\$\$
. - Best practices include using descriptive names and quoting variable expansions to prevent unexpected behavior.
Search Path
- The shell identifies commands by the first token after processing I/O redirection.
- Built-in commands, like
echo
andcd
, run directly without searching thePATH
. - If a command name isn't built-in, the shell searches executable files in directories listed in the
PATH
environment variable, a colon-separated list. - Directly executing a command with its full pathname bypasses
PATH
searching. - The first match found among multiple directories in
PATH
is executed. - A "command not found" error occurs if a command isn't located in
PATH
. - Avoid including the current directory (.) or adjacent colons (::) in
PATH
for security reasons; use./
to run commands explicitly. - The
which
command displays the directory containing a command, whilewhereis
finds commands and their man pages ignoringPATH
. - Modify
PATH
with the command:export PATH=\$PATH:/new/directory
, ensuring proper colon separation. - Set
PATH
at the beginning of shell scripts to ensure correct command execution.
Shell Scripts
- Shell scripting automates tasks by creating scripts with lists of commands executed by a shell program like Bash.
- Scripts can be interactive or text-file-based for greater flexibility.
- Execute scripts by calling the shell with the script file (
bash script.sh
orsh script.sh
) and make them executable withchmod +x script.sh
. - Scripts run in their own shell process, so changes do not affect the interactive shell.
- A standard script header includes a shebang (
#!/bin/bash -u
), setsPATH
, and specifiesumask
. - Use positional parameters (e.g.,
\$1
,\$2
) to access command-line arguments;shift
to process them sequentially, and"\$@"
for multiple arguments. - Redirection (
>
,>>
) and piping (|
) enable input/output manipulation;for
andwhile
loops allow iteration. - Ensure script security by validating inputs and managing permissions with
chmod
.
Command Substitution, Arithmetic, Exit
- Shell command substitution uses
\$(command)
to output command results into strings or variables (e.g.,echo "Your userid is \$(whoami)"
). - Integer arithmetic is performed using
\$(())
, supporting basic operations like addition and multiplication (e.g.,echo \$(( 2 + 2 * 9 ))
evaluates to 20). - For decimal calculations,
bc
is used with quoted expressions (e.g.,echo '22/7' | bc -l
outputs 3.14285714285714285714). - Commands return an exit status, ranging from 0 (success) to 255 (failures); captured with
\$?
. - A script inherits the exit status of the last executed command, and can set an explicit exit status with
exit N
(e.g.,exit 99
sets it to 99).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Dive into shell variables used in scripting with this Week 9 quiz. Learn about variable assignments, expanding variables, and the significance of quoting. Test your understanding of shell scripting concepts and best practices.