Week 5 Part 2 Flashcards

1
Q

how to create a shell variable?

A

myvar=”value string”

variables cannot start with a number and cannot have a space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to make a variable available to other shells?

A

export myvar=”value string”
or
myvar =”value string”
export myvar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to use variable values?

A

echo $myvar
echo ${myvar}
echo “${myvar}_something”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to remove a variable?

A

unset myvar
myvar =
(if we leave blank a variable after equal sign, we unset it)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how to test if a variable is set or not?

A

[[ -v myvar ]];
echo $?

Return codes:
0: success (variable myvar is set)
1: failure (variable myvar is not set)

[[ -z ${myvar + x} ]];
works opposite to ‘-v’ command above. If var is available, it will set the value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to print variable value when set or some other value if not set?

A

echo ${myvar:-“default”}
if myvar is set then displays its value. If not set, then displays default value

echo ${myvar:=”default”}
If myvar is set then displays myvar else sets myvar to defalut value and displays its new value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to reset value if variable is set?

A

echo ${myvar:+”default”}
If myvar is set then set the default value and display the new value else display null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to find the list of variables stored in the memory?

A

echo ${!H*}
List of names of shell variables that start with H

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to find the length of the string value of the variable?

A

echo ${#myvar}

displays length of the string value myvar. If myvar is not set it displays 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how to get a slice of the string value?

A

echo ${myvar:5:4}

displays 4 charcters starting from 5th position

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to remove matching pattern

A

echo ${myvar#pattern} - match once and remove

echo ${myvar##pattern} - match max possible times and remove

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to keep matching pattern within a variable?

A

echo ${myvar%pattern} -once
${myvar%%pattern} - multiple times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how to match and replace a pattern ?

A

echo ${myvar/pattern/string} - match once and replace with string.

echo ${myvar//pattern/string} - match max possible &replace with string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to replace matching pattern by location/

A

echo ${myvar/#pattern/string}
-match at beginning and replace with string

echo${myvar/%pattern/string}
-match at the end and replace with string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how to change case of the variable?

A

echo ${myvar,} - change first char to lower case

echo ${myvar,,} - change all chars to lower case

echo ${myvar ^} - change first char to upper case

echo ${myvar ^^} -change all chars to upper case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to restrict value types of variables?

A

( + removes restrictions
- gives restrictions)

declare -i myvar (integers)

declare -l myvar (only lower case chars)

declare -u myvar(only upper case chars)

declare -r myvar (read only variable) (+r is not possible)

16
Q

how are indexed arrays created in bash?

A

declare -a arr (declare arr as an indexed array)

$arr[0]=”value” (set element 0 to value)

echo ${arr[0]} (display value of 0th element of array)

echo${#arr[@]} (displays no of elements in the array)

echo${!arr[@]} (display all indices used)

echo${arr[@]}(display values of all elements of the array)

unset ‘arr[2]’ (delete element 2 in the array)

arr+=(“value”) (append an element with a value to the end of the array

17
Q

how are associative arrays (hash) declared?

A

declare -A hash
$hash[“a”]=”value”
echo ${hash[“a”]}
echo ${#hash[@]}
echo ${!hash[@]}
echo ${hash[@]}
unset ‘hash[“a”]’

18
Q
A
19
Q
A
20
Q
A
20
Q
A
21
Q
A
22
Q
A
23
Q
A