Bash, quiz 3 Flashcards
what is a shell?
A shell is a command interpreter, an interface between a
human (or another program) and the OS
what is the terminal?
The terminal is a program that opens a graphical window and
lets you interact with the shell.
what are the shell’s responsibilities?
Program execution
Variable and file name substitution
I/O Redirection
Pipeline Hookup
Environment Control
Interpreted Programming Languages
shell I/O redirection metacharacters
< > > > |
shell wildcard metacharacters
Asterix ? [ ]
shell other metacharacters
& ; $ ! \ ( ) space tab newline
shell substitutions/globbing
- Matches 0 or more of any character – ls *.c
? Matches any single character – ls file0?.c
[…] Matches any single character if it is in list provided
– ls file[0-9].c
what is a shell script?
A shell script is a file that contains shell commands
How do you specify the shell to execute the program?
The script must begin with #! (pronounced “shebang”) to identify shell to be executed
– #! /bin/bash
What are the steps to run a shell script?
To run:
– Make executable: chmod +x hello.sh
– Invoke via: ./hello.sh
What are the two types of variables that Bash supports?
Bash supports two types of variables:
– Local variables
– Environment variables,
How are environment variables set?
created using export[variable]
Environment variables are set by the system and can usually
be found using the env command
env [options] [varname=value] [command]
– Displays the current environment or modifies the specified variables
– Specified commands are executed in the new environment
what does the $PATH variable do?
Initially set when the shell is created (login process)
– Provides a list of available directories where an executable command may be found
List some shell scripting features
Full scripting language
– Conditional statements (if-then-else, case, …)
– Arithmetic, string, file, environment variables
– Input (prompt user, command-line arguments, …)
– Loop statements (for, while, do-while, repeat-until, …)
– Lists (and, or)
– Functions
– Traps
What is the syntax for declaring a local variable in bash?
Variable structure format
varname=value
– Variable name must begin with alphabetic or underscore character, followed by zero or more alphanumeric or underscore characters
– Note there should not be any spaces around the ‘=’ sign
– Variables are case-sensitive
– Do not use globbing metacharacters, such as ? and *, in your variable names
– Once defined, use by prefixing $ symbol to variable name
How do you export a Bash shell variable?
- Variables can be exported
– The value of the variable can be passed to other subshell - Export command format:
export [varname]
– Changing an exported variable in a subshell does not change the value in the parent shell
how do you destroy an exported variable? [bash scripting]
by using ‘unset’
How does ‘export’ with no arguments behave in a bash script
export with no arguments lists the variables that are exported
to the user’s shell
How do you read in user input, bash scripting
The read command allows you to prompt for input and store it in a
variable
* Syntax:
read varname [varname1] [varname2] … [varnameN]
– or
read –p “prompt” varname1 [varname2] … [varnameN]
* Input entered by user are assigned to varname1, varname2,
etc.
* If more input is entered than there are variables, the
remaining input will be assigned to the last variable