NDG Introduction to Linux II 2.21 | Chapter 1: Advanced Shell Features Exam Answers

This is NetAcad Cisco NDG Introduction to Linux II 2.21 – Chapter 1: Advanced Shell Features Exam Answers full 100% in 2023 – 2024. All answers have been verified by experts.

NDG Linux II 2.21 Chapter 01 Advanced Shell Features Exam Answers

  1. Which of the following is a valid variable assignment?

    •  alpha-init=5
    •  MY VAR= “set to one”
    •  2=xyz
    •  APP_VAR=’zero’
      Explanation & Hint:

      The valid variable assignment among the options you provided is:

      APP_VAR='zero'

      Variable names in many programming languages, including Bash scripting, typically cannot start with a number or contain spaces. They also usually cannot contain special characters other than underscores.

      So, the first three options are not valid variable assignments:

      1. alpha-init=5: Variable names cannot have hyphens in them.
      2. MY VAR= "set to one": Variable names cannot contain spaces.
      3. 2=xyz: Variable names cannot start with a number.

      The fourth option, APP_VAR='zero', follows the correct format for variable assignment in most programming languages, where you use the equals sign (=) to assign a value to a variable and the value is enclosed in single or double quotes.

  2. Shell variables are case sensitive. True or False?

    • True
    • False
    • Explanation & Hint:

      True. Shell variables are indeed case sensitive. This means that variables with different letter cases (uppercase and lowercase) are treated as distinct variables. For example, myVar and MYVAR would be considered two separate variables in a shell environment.

  3. Which of the following is not a valid variable name?

    •  NEWVARIABLE
    •  VAR_1
    •  _LPIC
    •  2_VAR
    • Explanation & Hint:

      The variable name “2_VAR” is not a valid variable name. In many programming languages and shell environments, variable names cannot start with a number. Therefore, “2_VAR” is not a valid variable name, while the other options (“NEWVARIABLE,” “VAR_1,” and “_LPIC”) are valid variable names.

  4. A new environment variable can be exported and assigned a value with a single command.
    True or False?

    • True 
    • False
    • Explanation & Hint:

      True. In Unix-like shell environments, you can export and assign a value to a new environment variable in a single command. The syntax for doing this varies slightly between different shell languages (such as Bash, sh, etc.), but the general concept remains the same.

      For example, in Bash, you can export and assign a value to a new environment variable using a single command:

      export MY_VARIABLE="some_value"

      This command both creates the environment variable MY_VARIABLE and assigns the value "some_value" to it, making it available to other processes launched from the current shell session.

  5. Which of the following is not a valid command for variable declaration?

    • export ENV_VAR=1 
    • newvar -x ENV_VAR=1 
    • typeset -x ENV_VAR=1 
    • declare -x ENV_VAR=1
    • Explanation & Hint:

      The command “newvar -x ENV_VAR=1” is not a valid command for variable declaration. The correct command for variable declaration and exporting it is the “export ENV_VAR=1” command.

      The other options:

      1. export ENV_VAR=1: This is a valid command for declaring a variable and exporting it.
      2. typeset -x ENV_VAR=1: This is a valid command for declaring a variable and exporting it. In some shells, typeset is used instead of declare to define variables, and the -x flag indicates exporting.
      3. declare -x ENV_VAR=1: This is a valid command for declaring a variable and exporting it. The -x flag is used to indicate exporting the variable.
  6. The ___________ command is used to display the value of a specific variable.

    • set 
    • var 
    • echo 
    • value
    • Explanation & Hint:

      The correct answer is:

      echo
      The echo command is used to display the value of a specific variable or any other text in the terminal. For example, you can use it to display the value of a variable like this:
      my_var="Hello, world!"
      echo $my_var

      This would output:

      Hello, world!
  7. Which command can be used to display both local and environment variables?

    • set 
    • declare -x
    • env 
    • export -p
    • Explanation & Hint:

      The command that can be used to display both local and environment variables is:

      set

      The set command, when used without any arguments, will display a list of all defined variables, including both local and environment variables, along with their values.

  8. Once a variable is declared, it cannot be deleted.

    True or False?

    • True
    • False
    • Explanation & Hint:

      False. Once a variable is declared, it can be deleted or unset in most programming and scripting languages, including shell environments.

      In shell scripting, you can unset a variable using the unset command. For example:

      my_var="Some value"
      echo $my_var # Outputs: Some value
      
      unset my_var
      echo $my_var # Outputs nothing (variable is unset)

      This removes the variable and its value from the environment. Keep in mind that once a variable is unset, you won’t be able to access its value anymore.

      However, do note that in some cases, variables might be automatically unset or cleared after the script or process finishes execution.

  9. Which of the following commands can be used to delete a variable?

    • rmvar 
    • declare 
    • set -r 
    • unset
    • Explanation & Hint:

      The command that can be used to delete or unset a variable is:

      unset

      The unset command in shell scripting is used to remove a variable and its value from the environment.

      The other options:

      1. rmvar: This is not a standard shell command for deleting variables. It’s not recognized as a valid command for this purpose.
      2. declare: The declare command is used to set attributes for variables, not to delete them.
      3. set -r: The set -r command is used to set the “readonly” attribute for variables, preventing them from being modified, but it doesn’t delete the variable.
  10. The PATH variable directories are searched __________ when executing a command.

    • Left to right 
    • In no particular order
    • Right to left
    • Including only the first 256 characters
    • Explanation & Hint:

      The PATH variable directories are searched left to right when executing a command. This means that the system searches for the command in the directories specified in the PATH variable from left to right, and it stops as soon as it finds the command in one of the directories. If the command is not found in any of the specified directories, you’ll receive a “command not found” error.

  11. Which of the following is not a valid option for executing a command that is not in the directories listed in the PATH variable?

    • Add the directory containing the command to the PATH variable
    • Copy the command to a directory listed in the PATH 
    • By typing the absolute or relative path to the command
    • Rename the command and its directory
  12. The command PATH=$PATH:/home/Alice does the following:

    • Creates a new PATH variable with only the /home/Alice directory as the value
    • Replaces the existing directories in the PATH with the /home/Alice directory
    • Appends the directory /home/Alice to the existing PATH variable 
    • Does nothing, because anything after the :is discarded
  13. Consider the following value of PATH variable:

    /bin:/usr/local/sbin:/home/tom/bin:/home/tom/test

    Tom modifies the my_scr.sh script, which is stored in the directory /home/tom/bin then places a copy of my_scr.sh in /home/tom/test. Executing my_scr.sh will:

    • Run the original version of the script in the /home/tom/bin directory 
    • Run the new version of the script placed in /home/tom/test 
    • Check the timestamp and run the latest version of my_scr.sh 
    • Not run at all, reports error
  14. The ___________ command can be used to check if a command already exists.

    • check 
    • type 
    • ex 
    • command
  15. Which of the following switches will include the full path in the prompt?

    • \H 
    • \w 
    • \e 
    • \W
  16. Defining the PS1 variable in an initialization file will not make changes to the prompt persistent between logins.

    True or False?

    • True
    • False
  17. If the HISTIGNORE='ls*' command is placed in the ~/.bash_profile file, which of the following commands would not be placed in the history list?

    • cat 
    • history 
    • cd 
    • ls -la
  18. Which of the following is not a purpose for creating an alias?

    • To create a short nickname for a long command or series of commands
    • To make commands run faster 
    • To include a command option by default
    • To create DOS-like commands
  19. The command to remove an existing alias from the current shell is:

    • unalias 
    • rmalias 
    • alias -d 
    • unset alias
  20. The best way to group multiple commands and pass arguments is to:

    • Concatenate them
    • Use functions 
    • Create an alias
    • Create an initialization file
  21. A function is given 3 arguments as input. The third argument of a function is identified as:

    • $PATH 
    • $1 
    • $3 
    • $arg3
  22. Changes in the __________ initialization file will affect all the users on the system.

    • Global 
    • Shell
    • Local
    • Network
  23. Local initialization files, by default, are stored in which directory?

    • /etc 
    • /bin 
    • /usr 
    • User’s home directory
  24. A login BASH shell executes which of the following additional files (compared to an interactive BASH shell)?

    • /etc/profile and either ~/.bash_profile or ~/.bash_login or ~/.profile 
    • ~/.bash_profile and ~/.bashrc 
    • /etc/profile and /etc/bashrc 
    • /etc/profile and ~/.bashrc
  25. Administrators use the __________ file to create key environment variables and set key system values for every user.

    • /etc/bashrc 
    • /etc/profile 
    • .profile 
    • /etc/config
  26. Sourcing is an effective way to test initialization file changes. Which of the following is used to source a file?

    • The touch command
    • The tilde ~ character
    • None of the above
    • Either the period . character or the source command
  27. Which script is executed when you exit the shell?

    • ~/.bashrc 
    • ~/.bash_logout 
    • /etc/bashrc 
    • ~/.bash_profile
  28. Typical BASH exit scripts are found in ~/.bash_logout and __________ files.

    • ~/.bash_profile 
    • /etc/bash_logout 
    • /etc/profile 
    • /etc/bashrc
  29. Most of the Linux commands used by regular users can be found in which directory?

    • /sbin 
    • ~/bin 
    • /usr/bin 
    • /usr/local/sbin