Shell for Beginners Amavect 2023-04-07 ABSTRACT This is an introductory guide about sh, command-line interfaces in general, and skills for learning on your own. This guide is for complete beginner programmers. Just enough to get you going and learning on your own. INTRODUCTION A "shell" is the common term for a command-line interface. The word is a metaphor for being around the operating system "kernel", also called the program supervisor. Why do programmers like the shell? Computers handle text more simply than graphics, so writing new command-line programs is much easier. The shell can pipe the output of one program to the input of another. Repeated actions can be placed into scripts, which automate those actions without typing everything out. The shell is dirty and pragmatic. Command-line programs may work very differently from one another, but often work similarly. Command names can be cryptic, but are often short and mnemonic. The shell is not always the best tool for a task, but it is capable of getting many jobs done quickly. It's not programming. It's another way of using the computer. START If you are using a Linux distribution, or another Unix-like operating system, run a terminal emulator, which should be preinstalled in your distribution. If you are using Windows, I recommend using msys2 and not cmd. The fundamentals will carry over, but writing for both is hard. Follow the install guide and run one of the executables. https://www.msys2.org/ Run one of the executables (or just pick ucrt64.exe). Comments begin with a # will not be executed. The shell will ignore characters after a # until the end of the line. Type the commands, and press enter to execute it! # The pwd command will print out the file path of your current directory. # pwd stands for Print Working Directory. pwd # The ls command will list out the file names in your current directory. # If there are no files, ls will output nothing! # ls stands for List. ls # The cd command moves to the parent directory, which is what .. means. # Then prints out the working directory, and lists what's in the working directory. # cd stands for Change Directory. cd .. pwd ls # Move to the root directory, which is what / by itself means. cd / pwd ls # List out all files, with a Lot more information. ls -l # There should be a home directory in the root directory. cd into it: cd home # The echo command will print back what you typed to it. echo lol # Environment variables are prefixed with $ # Print out the program search path. echo $PATH # Print out the home directory. echo $HOME # cd to the $HOME directory. cd # or cd $HOME # Assign your own variable. foo=hello echo $foo # The which command will find where in the $PATH a program is. # If it can't find the command, the command is likely built into the shell (a builtin). which ls which pwd which which which cd # this will fail. cd is a builtin. # Make a directory. mkdir mydirectory ls cd mydirectory # Write to some files with > echo hello > file1.txt echo world > file2.txt # Concatenate those files with cat, and print them out. cat file1.txt file2.txt # cat the files and pipe the output to another program # then print only matching lines with grep. cat file1.txt file2.txt | grep wor # grep stands for Global Regular Expression and Print. cryptic... # Concatenate those files with cat, and write them to a file. cat file1.txt file2.txt > file3.txt cat file3.txt # grep a file for only the matching string. grep rld file3.txt # Delete a file. rm file3.txt # Delete a directory, and everything in it. cd .. rm -r mydirectory # DO NOT DO THIS. IT WILL ERASE EVERYTHING: rm -rf / # I AM TELLING YOU THIS SO YOU DON'T EVER MAKE THIS MISTAKE # The fact that it's so easy to do it says how powerful shell commands are. BASICS A file is a thing made up by the operating system. Bytes of data can be read from a file or written to a file. A file can be executed as a program if it is the correct data format. Files are always synthesized by the operating system, and may represent data storage, communication hardware, or a way to talk to another program. A directory (or folder) is a file that points to other files. Directories fit inside directories, forming a mathematical tree with a root. A file path is a way of locating a file. On Unix and Linux: . means the current directory. .. means the parent directory / separates directories. / by itself means the root directory. There is only one root directory. Environment variables are strings of characters that can be read. Some environment variables are special. The $PATH variable is a list of directories where programs are searched. The $HOME variables is where cd without any arguments will move. The set builtin will print all defined environment variables. The anatomy of a command: command argument1 argument2 argument3 ... A command is a program, or a builtin. Arguments are passed to the program as a string. Arguments prefixed with a - are called flags. Flags may take an extra argument, or no argument at all. It's up to the program what to do with an argument. Perhaps the argument is a file name, or the argument changes program behavior. It's all arbitrary, but it should be documented in the manual page. HELP The shell is difficult, and it's impossible to remember everything. The manual pages explain literally everything about a program, though they are very technical. Don't be afraid! Learn how to read them! # rebuild the man page index (only need to do it once) mandb # read a manual page # press j k u d to go up and down. # To search for a word, press / then a word then press enter. # n for next match, N for next match. # press q to quit. man ls # https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html#top man sh # https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html#top man bash # not a POSIX program Some programs support help flags. program --help program -h Online reference for commands that every Unix-like operating system supports. http://shellhaters.org/ Another great online reference. Includes Windows CMD as well. https://ss64.com/ CONCLUSION I hope this helped, and I hope you had fun. Now get out there!