Shell

The shell is super useful to type commands. See Bash Script.

Resources:

bash is a relative of the shell.

  • Originally, meant for programmers to write shell scripts

Difference between executing a script ( ./) and sourcing (source) ?

The differences are:

  • When you execute the script you are opening a new shell, type the commands in the new shell, copy the output back to your current shell, then close the new shell. Any changes to environment will take effect only in the new shell and will be lost once the new shell is closed.
  • When you source the script you are typing the commands in your current shell. Any changes to the environment will take effect and stay in your current shell.

Source of truth: stackoverflow

Syntax to execute:

./myscript
  • This will execute myscript provided that the file is executable and located in the current directory. The leading dot and slash (./) denotes the current directory. This is necessary because the current directory is usually not (and usually should not be) in $PATH.
myscript
  • This will execute myscript if the file is executable and located in some directory in $PATH.

Syntax to source:

source myscript
  • This will source myscript. The file need not be executable but it must be a valid shell script. The file can be in current directory or in a directory in $PATH.
. myscript
  • This will also source myscript. This “spelling” is the official one as defined by POSIX. Bash defined source as an alias to the dot.

and for completeness:

exec myscript
  • This will terminate the current shell and then execute myscript in place of the terminated shell. That means when myscript is done there no shell to return to. exec is powerful but rarely needed.

Abstract

Short answer:

  • terminal = text input/output environment
  • console = physical terminal
  • shell = command line interpreter

Tip

Usually, if the flag has two dashes (ex: --help), you can use the shorthand version with one dash and one character (ex: -h).

Find folder with name Fast-CDR

find . -name Fast-CDR

How it works

Mostly notes from https://www.cis.rit.edu/class/simg211/unixintro/Shell.html

Although I had to teach some of this when I did the Air Cadet summer camp.

Source Files

System-wide shell initialization files are common on UNIX systems. These files can be “sourced” automatically by the shell and are typically setup by the System Administrator for the local environment.

Processes

  • Whenever you enter a command at the shell prompt, it invokes a program. While this program is running it is called a process. Your login shell is also a process, created for you upon logging in and existing until you logout.
  • UNIX is a multi-tasking operating system. Any user can have multiple processes running simultaneously, including multiple login sessions. As you do your work within the login shell, each command creates at least one new process while it executes.

To see your current shell’s processes

ps

Each process in a UNIX system has a unique PID - process identifier.

Use a common like

htop

to see how much each process is actually consuming in terms of CPU.

Basics

Useful Commands

I learned these in 2021-2022, became really comfortable with them.

  • cd - Takes you to the previous dir you were sitting in
  • pwd = “present working directory”
  • ls - list directory, see ls -lah
  • echo <file.txt> - built-in command, outputs the strings that are passed to it as arguments
  • cat <file.txt> - show the contents in one continuous stream
  • tee - The opposite of cat. cat reads from files whose names you pass it, but tee writes to them
  • man <cmd> - manual page for a command
  • mkdir <dir-name> - make a directory
  • touch <file.txt> - create a file, or if exists, update timestamps of when it was last modified
  • cp, mv, rm - copy, move, remove
    • RECOMMENDED: Add -iv flag to these commands (i for interactive, v for versbose)
  • diff <file1> <file2> - very useful, generates output describing how to change file1 into file2 (using patch)
  • cmp - useless, generates the first difference between the files
  • grep - software engineer’s best friend (Global ReGex Print)
    • grep -r <keyword>
    • ls | grep <keyword>
  • chmod - changes permissions of a file, see chmod

Tip

For most commands, you can do --help, which will list out the flags available to you.

Running Multiple Commands in one line

  1. Use ; No matter the first command cmd1 run successfully or not, always run the second command cmd2:
# cmd1; cmd2
$ cd myfolder; ls   # no matter cd to myfolder successfully, run ls
  1. Use && Only when the first command cmd1 run successfully, run the second command cmd2:
# cmd1 && cmd2
$ cd myfolder && ls  # run ls only after cd to myfolder
  1. Use || Only when the first command cmd1 failed to run, run the second command cmd2:
# cmd1 || cmd2
$ cd myfolder || ls  # if failed cd to myfolder, `ls` will run

Unix Pipes (super useful)

Pipes allow us to use the output of one program (via stdout) as the input to another (via stdin).

To pipe through vim, do

<command here> | vim - 

Globbing and quoting

Miscallaneous

Learned these misc stuff.

Apparently, there is .bash_aliases, where you can store dedicated aliases. The ~/.bashrc file automatically sources this file first.

I would just put it under .bashrc. It doesn’t really matter in the end.