Question

Difficulty: MediumBasic Scripting Languages and Constructs

A tier-2 helpdesk technician is auditing cross-platform system maintenance scripts used across Windows and Linux endpoints. The technician needs to verify the syntax rules for variable declaration and environment variable usage in these scripts. Which of the following statements correctly describe variable handling in PowerShell (.ps1) and Bash shell (.sh) scripts? (Select TWO).

  1. PowerShell scripts require a dollar sign prefix ($) when defining user variables and accessing environment variables.Answer
  2. B
    Bash shell scripts require a dollar sign prefix ($) both when assigning values to variables and when referencing them.
  3. Bash shell scripts assign values to variables without a leading symbol, but require a dollar sign ($) prefix to reference their contents.Answer
  4. D
    PowerShell scripts enclose environment variable names between percent signs (%) to evaluate their values.

Answer

PowerShell scripts use a dollar sign prefix ()fordefiningvariablesandaccessingenvironmentvariables,andBashshellscriptsassignvariablevalueswithoutaleadingsymbolwhilerequiringadollarsign() for defining variables and accessing environment variables, and Bash shell scripts assign variable values without a leading symbol while requiring a dollar sign () to evaluate or reference them.
PowerShell requires a dollar sign ()prefixforvariableinstantiationandenvironmentvariableaccess(suchas) prefix for variable instantiation and environment variable access (such as env:Path). In contrast, Bash shell scripts assign values to variables without a prefix (e.g., VAR=value) and require the dollar sign ()onlywhenreferencingthevariablesvalue(e.g.,echo) only when referencing the variable's value (e.g., echo VAR).

Step-by-Step Solution

1
Analyze PowerShell (.ps1) variable syntax rules
Confirm that PowerShell uses the dollar sign ()symbolforvariableinstantiationand) symbol for variable instantiation and env: for environment variable access.
PowerShell consistent syntax relies on the dollar sign ($) for variable scope and access.
2
Analyze Bash shell (.sh) variable syntax rules
Confirm that Bash assigns variables using key=value without a dollar sign ()ontheleftside,butrequiresadollarsign() on the left side, but requires a dollar sign () to read the value.
Adding a dollar sign ($) during Bash variable assignment causes a syntax error.
3
Evaluate distractor statements regarding batch (.bat) syntax
Identify that enclosing variables with percent signs (%VAR%) belongs to Windows Batch scripting, not PowerShell.
PowerShell does not use percent signs for environment variable expansion.

Key Concept

Variable Declaration and Syntax Rules in Scripting Languages
Estimated Time:1m 30s
Rate this question