Skip to content

Instantly share code, notes, and snippets.

@cpacia
Last active July 15, 2021 16:12
Show Gist options
  • Save cpacia/d0b29c0506b20b0c32cba0d7f1931c1a to your computer and use it in GitHub Desktop.
Save cpacia/d0b29c0506b20b0c32cba0d7f1931c1a to your computer and use it in GitHub Desktop.
Terminal Basics

TERMINAL BASICS

In this tutorial we are going to cover some basics of using the terminal. Hopefully you will be using either linux or mac as the terminal syntax will be the same as I cover here. On windows you can do the same things but the syntax is different. You'll need google how to do these things on windows as I don't know them off the top of my head.

Browsing Files

When you open the terminal you will see a comand prompt. Mine looks like this:

chris@chris-Yoga:

Think of your terminal as sort of a primitive file explorer. When you open a file explorer application you can look inside directories and browse around. You can do this from the terminal as well. By default, when you open the terminal you start in your computer's Home directory. You can view the files and/or directories inside this directory by typing the ls command (and pressing enter).

chris@chris-Yoga:~$ ls
Desktop    Downloads  GoLand-2021.1.1  OpenBoard  Public  Templates  workspace
Documents  go         Music            Pictures   snap    Videos

Mine I only have directories in here so that's all you see. You can enter a directory by typing cd followed by the directory name. For example to enter the Downloads directory you would type:

chris@chris-Yoga:~$ cd Downloads

Notice now my terminal prompt changes to show I'm inside the Downloads directory:

chris@chris-Yoga:~/Downloads$

From here I could either use the ls command to list the files and directories inside my Downloads directory or I could enter another subdirectory by using cd again.

For example I have a sub directory called recordmydesktop-0.4.0 inside Downloads directory for a program I recently downloaded and unzipped. To enter this I would type:

chris@chris-Yoga:~/Downloads$ cd recordmydesktop-0.4.0

And my terminal would change to show the full path of the directories I'm inside:

chris@chris-Yoga:~/Downloads/recordmydesktop-0.4.0$ 

To go back one level and return to the Downloads directory I would type cd ..

For example:

chris@chris-Yoga:~/Downloads/recordmydesktop-0.4.0$ cd ..

Returns me to the Downloads directory.

chris@chris-Yoga:~/Downloads$

From inside any directory you can type cd and it will return you to the home directory.

You also don't need to cd into directory one directory at a time. I could, for example, jump straight from my Home directory to the recordmydesktop-0.4.0 directory by typing the full path including the Downloads directory.

For example:

chris@chris-Yoga:~$ cd Downloads/recordmydesktop-0.4.0

Jumps me straight into recordmydesktop-0.4.0

chris@chris-Yoga:~/Downloads/recordmydesktop-0.4.0$

You can also jump to other directories without first going back to your home directory. For example, to jump from /Downloads/recordmydesktop-0.4.0 to my workspace directory which is in my home directory I could type:

chris@chris-Yoga:~/Downloads/recordmydesktop-0.4.0$ cd $HOME/workspace

Which moves me right to workspace.

chris@chris-Yoga:~/workspace$ 

Alternatively you could ~/workspace instead of $HOME/workspace as ~/ is shorthand for your home directory.

Executing Programs

To execute (run) a program you can cd into the directory containing the program. And then type ./ followed by the filename.

For example:

chris@chris-Yoga:~$ cd go/bin
chris@chris-Yoga:~/go/bin$ ./meep

This runs a program called meep that I wrote. The file was named meep and was found inside my $HOME/go/bin directory. So I cded into that directory and ran the file.

Notice if you are inside the directory with the file you want to run you have to type ./ before the filename.

Alternatively you could run the file from another directory. If you do it this way you do not use the ./ but instead type the full path to the file.

For example, from inside my Home directory I can type:

chris@chris-Yoga:~$ go/bin/meep

And that will also run my program.

Sometimes you might get an error message like this when trying to run a program:

chris@chris-Yoga:~$ ./test.sh
bash: ./test.sh: Permission denied

Sometimes you have to tell the operating system to allow this program to by treated as an executable file before you can run it. In my case it thought that test.sh file was just a generic text file and not a program. You can do this by typing chmod +x followed by the filename. That will make the file executable.

In my case:

chris@chris-Yoga:~$ chmod +x test.sh

Alternatively you could open your file explorer, right click on the file, click permissions, and then check the box that says "Allow executing file as a program".

Some programs do not immediately exit back to the terminal prompt after your start them. Instead they continue running until you explicitly stop them. To stop the program and return to the command prompt type ctrl + c.

Your Path

You have a system wide variable called your $PATH which holds one or more directory paths. If we want to avoid having to either cd into a directory or type the full path to our program to run it and instead we want to run the program from any directory, we can add the program's directory to our $PATH.

For example I have $HOME/go/bin in my $PATH. This means I can run my meep program (which is inside $HOME/go/bin) simply by typing meep into the terminal from any directory.

chris@chris-Yoga:~$ meep

When I type the name of a program like that, the operating system will look inside the $PATH directories to see if a file by that name exists in any of those directories and runs it. The OS looks inside the directories in the order they are listed in the $PATH. So if more than one file with the same name exists it will execute the first one found.

Program Arguements

When you start a program you can pass data into the program as you start it. You do this by just typing your arguements after the program name separated by a space. For example with my meep program:

chris@chris-Yoga:~$ meep thisIsAnArgument thisIsAnotherArgument thisIsYetAnotherArguement

When the program starts thisIsAnArgument, thisIsAnotherArgument, and thisIsYetAnotherArguement will be passed into the program. The program can then decide what to do with those arguements. It can chose to ignore them if it likes.

Often times we use these arguements to pass into option flags to tell a program how to run. For example, most programs have a help option that will print a help menu and exit. To see the help menu you typically pass in either --help or -h. Notice the use of -- and - these aren't strict necessary when passing in command line arguements to a program. A program could theoretically be written in such as way that dashes are not needed, but it has become a convention that dashes are used for options. If I do this on my meep program I get:

chris@chris-Yoga:~$ meep --help
Usage:
  meep [OPTIONS] <debug | execute>

Application Options:
  -v, --version  Print the version number and exit

Help Options:
  -h, --help     Show this help message

Available commands:
  debug    Enter Debugger
  execute  Execute a script

Notice that --help printed the help menu and exited the program back to the terminal prompt. It does that because I programmed it to do so when the first argument passed in is --help.

In addition to options some programs also interpret command line arguments as commands. If you look at the help menu for my meep program it says it offers two commands debug and execute. To run either of these commands I would type the command name in as an arguement.

For example:

chris@chris-Yoga:~$ meep execute

Sometimes programs have help menus for each of their commands. For example if I type meep execute --help I get:

meep execute --help
Usage:
  meep [OPTIONS] execute [execute-OPTIONS]

Execute a script, print the result and exit.

Application Options:
  -v, --version        Print the version number and exit

Help Options:
  -h, --help           Show this help message

[execute command options]
      -t, --tx=        the full transaction hex or BCH mainnet txid. If only a
                       txid is provided the transaction will be looked up via
                       the RPC server.
      -i, --idx=       the input index to debug
      -a, --amt=       the amount of the input (in satoshis) we're debugging.
                       This can be omitted if the transaction is in the BCH
                       blockchain as it will be looked up via the RPC server.
      -s, --pkscript=  the input's scriptPubkey. This can be omitted if the
                       transaction is in the BCH blockchain as it will be
                       looked up via the RPC server.
          --rpcserver= A hostname:port for a gRPC API to use to fetch the
                       transaction and scriptPubkey if not providing through
                       the options. (default: bchd.greyh.at:8335)

This is telling how to use the execute command. Specifically it says that this command will execute a bitcoin cash script. It has options to pass in the transaction, index, amount, script, and rpc server IP address. Notice this follows a pretty standard convention in that our options can take either a short form or a long form. For example to use the amount option I could use the short form -a 7800000 or the long form --amt=780000. The long form (with two dashes) requires use of the equal sign to separate the option from value whereas the short form (single dash) only requires a space.

The full command may look something like this:

meep debug --tx=048e890c1931a8c3f908d5826943c47021c5bfebc6c8ef96684207c53cfa7ea3 --idx=0 --amt=100000 --pkscript=a914a350ea021c1e576f1b5b66306f1532ab0b53f12f87

At the end of the day the program requires the data passed in via command line arguements to be formated in a way that it can interpret and the help menu typically tells you specifically how to format it to make the program do different things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment