Bash Flashcards

1
Q

what are the two modes of Bash?

A

interactive , or non-interactive

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

Creating and writing to txt file

A

ex #bash command to run the ex program
i #ex command to insert text
random text
. # line with just a dot tells ex to stop inserting text
w filename.txt # ex command to write the text to a file
q #ex command to quit the program

cat filename.txt #cat program shows contents of the file

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

interactive mode

A

bash waits for your commands before performing them . While bash is performing the commands, you can’t interact with the bash shell

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

have to mark file as executable before the kernel allows it to run as a program.

A

chmod +x file.txt

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

have to mark file as executable before the kernel allows it to run as a progrm.

A

chmod +x file.txt

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

non interactive mode

A

bash can execute scripts (pre-written series of commands). scripts are saved in files and need to be run once

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

What is Bash?

A

1) Bash is a shell program designed to listen to my commands and do what I tell it to.
2) Bash is a simple tool in a vast toolbox of programs that lets me interact with my system using a text-based interface.
3) The bash shell is a binary program that runs either interactively or non-interactively, usually in a text-based interface provided by a terminal emulator program.

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

hashbang line at the top of the bash script tells the kernel what interpreter it needs to use to understand the language in the file and where to find it (– the bash interpreter). MUST specify an absolute pathname to any program that understands the language in your file and can take a single argument.

A
example: 
#!/usr/bin/env bash
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Bash gets commands by reading lines. As soon as it’s read enough lines to compose a complete command, bash begins running that command. Usually, commands are just a single line long. An interactive bash session reads lines from you at the prompt. Non-interactive bash processes read their commands from a file or stream. Files with a hashbang as their first line (and the executable permission) can be started by your system’s kernel like any other program.

A

TLDR - commands

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

bash shows a special prompt [>] when the line you have inputed doesn’t contain enough information for bash to execute

A

i.e. an if loop
$if blah; then #starts, but isn’t finished
> echo blah
>else
> echo nah
> fi #ends if block, so bash will now run the whole block

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

kernel

A

a program that communicates with your hard drive

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

program

A

a set of pre-written instructions that is executed by your system’s kernel. It gives directions to the kernel directly. When you “run” or “execute” a program, your kernel loads its pre-written instructions (its code) by creating a process for your program to work in. Your program can run many times simultaneously, each of those instances are running processes of your program.

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

file descriptors

A

A process has a few hooks to the outside world via something called file descriptors. These are essentially plugs we use to connect processes to files, devices or other processes.

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

standard input

A

File descriptor 0 is called standard input. This is where most processes receive their input from. By default, processes in your terminal will have their standard input “connected” to your keyboard. More specifically, to the input your terminal program receives.

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

bash is a lax language interpreter, which means it will permit you to write ambiguous commands. its syntax will not prevent you from writing commands that do things that are not what they seem. It is solely your responsibility to learn the syntax adequately, recognize the pitfalls and pick up the discipline to stick to the practices that avoid buggy code consistently.

A

TLDR lang

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

Bash Commands: Simple Commands

Syntax:
[var=value …] name [arg1 …] [redirection …]

A

most common kind of command. specifies the name of a command to execute, along with an optional set of arguments, environment variables, and file descriptor redirections. before the command’s name you have the option of putting a few var assignments. these apply to the environment of this command only. the command’s name is the first word after the optional assignments. bash finds the command with that name and starts it. A command’s name is optionally followed by a list of arg words, the command arguments. Finally, a command can also have a set of redirection operations applied to it. [redirections are the operations that change what the file descriptor plugs point to. they change the streams that connect to our command processes.]

17
Q

Bash Commands: Pipelines

Syntax:

[time [-p]] [ ! ] command [ [ /|/&] command2 …]

A

a convenient way of ‘connecting’ two commands by way of linking the first process’ standard output to the second process’ standard input. this is the most common way for terminal commands to talk to one another and convey information.

we rarely use the time keyword, but it is convenient for finding out how long it takes to run commands. the ! keyword doesn’t have much to do with connecting commands. the first [command] and the second [command2] can be any type of command from this section. bash will create a SUBSHELL for each command and set up the first command’s standard output file descriptor such that it points to the second command’s standard input file descriptor. the two commands will run simultaneously and bash will wait for both of them to end.

the pipe symbol [ | ] tells bash to connect the output of the first to the input of the second command.

can also use the |& symbol inbetween commands to indicate that we want not only the standard output of the first command, but also its standard error to be connected to the second command’s input (usually undesirable, since the standard error file descriptor is normally used to convey messages to the user)

18
Q

Bash Commands: Lists

Syntax:
command control-operator [command2 control-operator …]

A

a list is a sequence of other commands. commands in lists are separated by a control operator which indicates to bash what to . do when executing the command before it.

the simplest control operator is just starting a new line, which is equivalent to [ ; ], and tells bash to just run the command and wait for it to end before advancing to the next command in the list.

another control operator [ | | ] tells bash to run the command before it as it normally would, but after finishing that command move to the next command only if the command before it failed. if the command before it didn’t fail, the [ | | ] operator will make bash skip the command after it. (useful for showing error messages when a command fails).

19
Q

Bash Commands: Compound Commands

Syntax:
if list [ ;| (newline) ] then list [ ;| (newline) ] fi
or:
{ list ; }

A

compound commands have special syntax inside them. they can do many different things but behave as a single command in a command list.

20
Q

Bash Commands: Coprocesses

Syntax:
coproc [ name ] command [ redirection … ]

A

a coprocess allows you to easily run a command asynchronously (without making bash wait for it to end) and also set up some new file descriptor plugs that connect directly to the new command’s input and output.

21
Q

Bash Commands: Functions

Syntax:
name () compound-command [ redirection ]

A

when you declare a function in bash, you’re essentially creating a temporary new command which you can invoke later in the script. this is a great way to group a list of commands under a custom name for convenience when you repeat the same task more than once in your script.

you begin by specifying a name for your function. this is the name of your new command, you’ll be able to run it later on by writing a simple command with that name.

after the command name go the () – SHOULD ALWAYS BE EMPTY – not declaring the args! () simply denote that you’re declaring a function.

next comes the compound command that will be executed each time you run the function.

to change the file descriptors of the script for the duration of running the function, you can optionally specify the function’s custom file redirections.

22
Q

Bash commands tell bash to perform a certain unit of work. these units of work cannot be subdivided: bash needs to know the whole command to be able to execute it. there are different kinds of commands for different types of operations. some commands group other commands into blocks or test their result. many command types are syntax sugar: their effect can be achieved differently, but they exist to make the job easier.

A

TLDR- commands

23
Q

standard output

A

File descriptor 1 is called standard output. This is where most processes send their output to. By default, processes in your terminal will have their standard output “connected” to your display. More specifically, your terminal program will display this output in its window.

24
Q

standard error

A

File descriptor 2 is also called standard error. This is where most processes send their error and informational messages to. By default, processes in your terminal will have their standard error “connected” to your display, just like standard output. It’s important to understand that standard error is just another plug, just like standard output, which leads to your terminal’s display. It isn’t dedicated to errors, in fact bash uses it for most of its informational messages as well as your prompt!

25
Q

process

A

a program typically lives on your disk, waiting to be started. When you run the program, the kernel loads the code and creates a process for your program to work in

26
Q

can your program run many times?

A

Holy yes! Your program can run many times simultaneously and each instance of the program is running in a different process

27
Q

stream

A

A stream is information (specifically, bytes) flowing through the links between files, devices and processes in a running system. They can transport any kind of bytes, and the receiving end can only consume their bytes in the order they were sent. If I have a program that outputs names connected to another program, the second program can only see the second name after first reading the first name from the stream. The stream cannot be rewound and the name cannot be re-read.

28
Q

TL;DR

A

Each time a program is started, the system creates a running process for it. Processes have plugs, called file descriptors which allow them to connect streams that lead to files, devices or other processes.