Bash Basics - For New Users
If you're new to Linux and Unixes in general, then you are new to Bash. Bash is the command line shell that is the default for most Linux distros.
What is Bash
You probably already know that the command line is the most basic way of interacting with the operating system. Bash is the default command line and is very powerful on Linux in fact the operating system actually boots using mostly Bash commands. If it can boot an operating system imagine what you can do with it. It's used to automate complex jobs through scripting, when learned it can be used more quickly and more effectively than the GUI for many tasks, and it allows you to access many of the top-quality tools that don't have a GUI interface.
Getting into Bash
You can get to the command line in many ways. You can choose the "Terminal" or "Console" item from the GUI menu, this will launch an X-Windows terminal emulator with a bash shell running inside. You can also use one of the virtual text consoles. Pressing Ctrl-Alt and a function key at the same time switches to another virtual console. Most GUIs have Ctrl-Alt-F2 as a text console. To return to the GUI try Ctrl-Alt-F1 through to F8 to find the GUI console. Finally another way to obtain a shell prompt is over the network using SSH. With SSH you log into your computer using the same user and password as your GUI and are presented with a Bash shell.
Core ideas of Bash
Using the command line can be quite simple. The term "command line" is suitable, you type a command, press Enter and Bash executes the command. Most commands take additional arguments. Consider the cd command, this command changes the current directory, i.e. the directory that subsequent commands will work in. Think of it like opening a new folder in a GUI environment. The cd command can work without any arguments, entering cd on the command line and pressing the Enter key will change the current directory to your user's home directory. That's useful, but you first had to be somewhere else. To get somewhere else type in a directory name after the cd command, e.g. cd /tmp. This changes the current directory to /tmp, a system-wide directory for holding all sorts of temporary files.
You'll notice something about the cd command we just used. The command was first on the line and the argument after it. You'll also notice that a space was used to separate the command from the argument, this is required.
Let's take a look at another command. The cp command copies files and takes two or more arguments. It takes one or more files to copy and a destination. The destination can be either a new file name in which case the file is renamed, or it can be a destination directory to which the file(s) are copied in which case the file names don't change.
cp my_report.txt MyReport.txt cp baked_beans.txt corn_bread.txt documents/recipies
What really are commands?
Commands come in two general varieties. There are built-in commands that are programmed into the Bash shell. These are often simple commands or ones that are designed to affect how Bash works. The command we used above, cd is a built-in command. When Bash sees that you've typed a built-in command it interprets that command itself.
The other type of command is an executable program. This can be a compiled executable that exists as a binary, machine language program, or it can be a script or any other type of interpreted program. Scripts are text files that are interpreted by another program. Some scripts are Bash scripts written to be interpreted by the Bash shell.
All non-built-in commands are files.
We have seen that commands can take arguments, they can also take options. What is the difference between an option and an argument? Well there really isn't anything technically different, options can be thought of as a specific type of argument. It's a customary thing really. Options are switches that change the function of the command where a arguments are thought of as the object the command is working with. This isn't an absolute truth since it's up to the programmer of the command to determine how the arguments are used.
Options are arguments that begin with one or more hyphens (-). They change how the command operates. Take the -i of the mv command, which moves files. The -i switch makes the mv command prompt before overwriting files.
mv -i baked_beans.txt corn_bread.txt documents/recipes
Some options themselves require an additional argument. Take the mail command, it can be used to send email. The arguments are a list of email addresses to send the email to. Optionally subject line text can be specified on the command line:
mail -s "Buy Viagra Now" firstname.lastname@example.org email@example.com
In the above case the -s option requires the subject line text to follow. I had to put the text in quotes because it includes spaces. If I didn't then Bash would think that the subject line was "Buy" and the other words would be email addresses.
More recently programmers have started using longer option names, either because it makes them more memorable, or because they ran out of single letter options. These new-style options customarily begin with two hyphens (--).
We can see this in the ls command. This command lists files and is one of the oldest commands and has a huge list of possible options. We will use the -all option. Normally ls will not list hidden files, the --all option causes ls to display hidden files as well.
There are a lot of command available for use in Linux. On my laptop, which doesn't have everything installed, there are over 4,000 commands. You can see a long list of commands by looking in the /bin and /usr/bin directories.
ls /bin /usr/bin
These are the main directories for user programs. If you want to know more about a program then use the man program, it displays the manual page for a specific command. To see the man page for the ls command type man ls.
You can find a list of the built-in commands by looking at the man page for Bash (man bash) and searching for the SHELL BUILTIN heading (hint: while viewing the page type /^SHELL BUILTIN and press Enter.)