Bash Flashcards
what are the two modes of Bash?
interactive , or non-interactive
Creating and writing to txt file
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
interactive mode
bash waits for your commands before performing them . While bash is performing the commands, you can’t interact with the bash shell
have to mark file as executable before the kernel allows it to run as a program.
chmod +x file.txt
have to mark file as executable before the kernel allows it to run as a progrm.
chmod +x file.txt
non interactive mode
bash can execute scripts (pre-written series of commands). scripts are saved in files and need to be run once
What is Bash?
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.
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.
example: #!/usr/bin/env bash
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.
TLDR - commands
bash shows a special prompt [>] when the line you have inputed doesn’t contain enough information for bash to execute
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
kernel
a program that communicates with your hard drive
program
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.
file descriptors
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.
standard input
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.
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.
TLDR lang