Shell Scripting

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/50

encourage image

There are no tags or description

Looks like no tags are added yet.

51 Terms

1
New cards

What is a shell script?

A file of commands run by a shell

2
New cards

What is the first line #!/bin/bash called?

A shebang

3
New cards

How do you run a script in the current folder that is executable?

./script.sh

4
New cards

How do you run a script with Bash without execute permission?

bash script.sh

5
New cards

How do you write a comment in Bash?

Start it with #

6
New cards

How do you assign the value 5 to x?

x=5

7
New cards

Are spaces allowed around = in a Bash assignment?

No

8
New cards

How do you read a variable's value?

$x or ${x}

9
New cards

Why quote "$var"?

To prevent word splitting and filename expansion

10
New cards

What do single quotes do?

Keep text literal

11
New cards

What do double quotes allow?

Variable and command substitution

12
New cards

What does $(command) do?

Substitutes the command's output

13
New cards

What does $((2 + 3)) produce?

5

14
New cards

What is $0?

The script or shell name

15
New cards

What is $1?

The first argument

16
New cards

What is $#?

The number of arguments

17
New cards

What does "$@" represent?

All arguments as separate words

18
New cards

What does shift do?

Moves positional arguments down

19
New cards

How do you read user input into name?

read -r name

20
New cards

What does read -p do in Bash?

Shows a prompt before reading

21
New cards

Which keyword ends an if block?

fi

22
New cards

Which keyword starts the alternative branch of an if?

else

23
New cards

Which keyword adds another condition to an if?

elif

24
New cards

What does [ -f file ] test?

Whether a regular file exists

25
New cards

What does [ -d folder ] test?

Whether a directory exists

26
New cards

What does [ -e path ] test?

Whether a path exists

27
New cards

What does [ -z "$x" ] test?

Whether the string is empty

28
New cards

What does [ -n "$x" ] test?

Whether the string is not empty

29
New cards

What does -eq compare?

Whether two integers are equal

30
New cards

What does -lt mean?

Less than

31
New cards

What does -gt mean?

Greater than

32
New cards

Which keyword ends a for or while loop?

done

33
New cards

How do you loop over all .txt files?

for f in *.txt; do … done

34
New cards

What does a while loop do?

Repeats while its condition succeeds

35
New cards

What does an until loop do?

Repeats until its condition succeeds

36
New cards

What does break do?

Exits a loop

37
New cards

What does continue do?

Skips to the next loop iteration

38
New cards

Which keyword ends a case block?

esac

39
New cards

How do you define a function named greet?

greet() { …; }

40
New cards

What does local do in a Bash function?

Limits a variable to the function's scope

41
New cards

What does return set in a function?

Its exit status

42
New cards

What exit status means success?

0

43
New cards

What does exit 1 indicate?

The script ended with an error

44
New cards

What does set -u do?

Treats use of unset variables as an error

45
New cards

What does set -o pipefail do?

Makes a pipeline fail if any command in it fails

46
New cards

What does bash -n script.sh check?

Syntax without running the script

47
New cards

What does bash -x script.sh show?

Commands as they execute

48
New cards

Which tool finds common shell script mistakes?

ShellCheck

49
New cards

What does trap do?

Runs commands when a signal or shell event occurs

50
New cards

How do you create an array in Bash?

items=(one two three)

51
New cards