Bash Scripting Flashcards

1
Q

(Single Parentheses)

A

Runs the command(s) in a subshell and returns a single exit code. Any variables declared only exist in this subshell.

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

((Double Parentheses))

A

– Used for integer arithmetic. Any variables modified inside the brackets stick.
– It doesn’t return a value, but if the result is non- zero, it returns a 0, i.e., a “successful” exit code
– If the result inside is zero, it returns an exit code of 1, i.e., a “not successful” exit code

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

$( Dollar Single Parentheses )

A

This is used for interpolation of the subshell command output into a string (instead of just an exit code if without the dollar symbol)

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

$(( Dollar Double Parentheses ))

A

This performs arithmetic interpolation of the command in the brackets, e.g., the output of the command is put into a string as opposed to just the exit code. Strictly for integer arithmetic.

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

[ Single Square Brackets ]

A

– This is a shorthand version of the built-in test command. The commands inside are executed and checked if true or false.
– Strings of zero length are false and strings of length 1 or more are true

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

[[ Double Square Brackets ]]

A

– Also used for true/false testing
– Supports regular expression matching
– Preferable to use over single brackets in the vast majority of use cases

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

{ Single Curly Braces }

A

– Used for expansion
– For example, echo {1..10} makes a range
– echo “This is “{great,useful}”.” returns This is great. This is useful.

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

${dollar braces} (note no spaces)

A

This is for variable interpolation when normal string interpolation can’t work correctly

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

Positional Arguments

A

– Arguments passed to a script are processed in the same order in which they’re sent.
– The indexing of the arguments starts at one, and the first argument can be accessed inside the script using $1. Similarly, the second argument can be accessed using $2, and so on.
– The positional parameter refers to this representation of the arguments using their position.

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

Flags

A

– Using flags is a common way of passing input to a script.
– When passing input to the script, there’s a flag (usually a single letter) starting with a hyphen (-) before each argument.\– For example, ls -l lists all the files in a directory – the -l is a flag to the ls command
– The bash shell provides a built-in getopts command to help with handling flag options

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

Integer comparison operators

A

-eq is equal to
-ne is not equal to
-gt is greater than
-ge is great than or equal to
-lt is less than
-le is less than or equal to

  • Usage example: if [ “$a” -gt “$b” ] then
  • Note <, <=, >, >= can be used within double parentheses, e.g., ((“$a” > “$b”))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

String Comparison Operators

A

= or == is equal to
!= is not equal to
< is less than, in ASCII alphabetical order
> is greater than, in ASCII alphabetical order
-z string is null
-n string is not null

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

File Test Operators

A

-e file exists
-f file is a regular file (not a dir/device)
-d file is a directory
-h or -L file is a symbolic link
-r file has read permission (for current user)
-w file has write permission (for current user)
-x file has execute permission (for current user)

  • Example usage: if [ -d “$path” ] then
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Flow of Controls: If Statements

A

if CONDITION then
COMMANDS
else
OTHER_COMMANDS
fi

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

Flow of Controls: While Loops

A

while [ CONDITION ]
do
COMMANDS
done

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

Flow of Controls: For Loops

A

for VALUE in LIST
do
COMMANDS
done

17
Q

Functions

A

function_name () {
<commands>
}</commands>

OR

function function_name {
<commands>
}</commands>