Bash for beginners
BASH (Bourne-Again Shell) is a command line interpreter for Unix-like operating systems. It executes commands read from a standard input (runs in a text window where the user types commands that cause actions) and from a file, called a shell script. Bash is distributed as default shell for most Linux distributions, Apple macOS and it is also available on Windows 10. In this article we will look into basic Bash command line commands and cover the very basics of shell scripts.
Table of Contents
1. Basic bash command line commands
First we're going to look into executing basic interactive command line commands by using a terminal window. Start by opening a terminal window. Your terminal emulator should look similar to this:
1.1. Basic syntax
Commands in Bash consist of a command name followed by options and arguments. Depending on the command, arguments can represent a file name, directory or an expression.
Basic Bash syntax looks like this:
$ command_name [-options] [arguments]
Example:
To list files and directories in our Documents folder by file size, we type the following:
$ ls -S Documents
- we start with the name of the command: ls
- command name is followed by an option to sort the content of the folder by file size, largest first: -S
- at the end we add the name of the folder we're interested in: Documents
1.2. Simple commands
1.2.1. Getting help
The most important thing when working with Bash is to know how to find information about the commands you'd like to use.
- To access a manual page of a command, use man.
user@host:~$ man [command_name]
- To get an overview of possible commands, use info or help. If you would like to focus on specific command, add the command name after info or after help command.
user@host:~$ info
user@host:~$ help
1.2.2. Navigating directories and listing content with pwd, cd and ls commands
- To print the name of the current directory, use pwd command:
user@host:~$ pwd
/home/user
- To move to another directory, use cd command:
user@host:~$ cd existing-folder
After using the change directory (cd) command, you will be moved to the folder named existing-folder:
user@host:~/existing-folder$
If the folder doesnt exist, you will see this message:
bash: cd: existing-folder: No such file or directory
- To list all the directories and files in your current folder, use ls command:
user@host:~/existing-folder$ ls -a
Here we used an option -a, which makes sure that all the folders and files are displayed, including the ones starting with a dot (.).
1.2.3. Managing directories and files with cp, mv, mkdir, rmdir and rm commands
- To copy a file, use cp command:
user@host:~$ cp file1.txt file2.txt
In this case we made a copy of file1.txt and named it file2.txt
- To move a file (or rename it), use mv command:
user@host:~$ mv file1.txt existing-folder/
file1.txt will be moved to existing-folder directory.
(Hint: You can check that by moving to existing-folder by using cd command and then checking the content of the folder by using ls command)
- To create a new directory, use mkdir command:
user@host:~$ mkdir new-dir
- To remove/delete an empty directory, use rmdir command:
user@host:~$ rmdir new-dir
- To remove/delete a file, use rm command:
user@host:~$ rm file2.txt
1.2.4. File and content search and analysis with file, grep and find
- To determine a file type, use file command:
user@host:~$ file file2.txt
- To find patterns in a file, use grep command:
user@host:~$ grep hello file2.txt
The command above will return a result to standard output if file2.txt contains text "hello".
- To find a file, use find command:
user@host:~$ find file*
In this case the star at the end of the file name represents a wildcard, which means that any number of any characters can follow the character sequence "file2". For example, if we have two files in our current directory named file1.txt and file2.txt, the above command will list both files.
1.3. Complex commands
1.3.1. Pipeline
To perform complex tasks, we can use a pipeline by connecting one command's output to another command's input. We do that by using the symbol "|".
Example:
user@host:~$ ls | grep file
If we have two files in our current directory, named file1.txt and file2.txt, the output of the above command will be:
file1.txt
file2.txt
What happens here is that the ls command's output is redirected to grep file command's input. This means that grep file gets a list of all files and directories in current directory and then searches through this list for occurence of a string "file".
1.3.2. Redirecting the output
Another useful concept is redirecting the output of a command to an input.
Example:
user@host:~$ ls > file.txt
This command will write the list of files and directories, which would be the output of ls command, from the current directory to a file file.txt.
2. Bash shell scripts
To perform a predefined sequence of commands, we can write the commands to a file and then execute the file. A Bash script must always beginn with #!/bin/bash.
To understand how to write shell scripts, we've written a simple script named hello-bash.sh, which prints out the output "Hello, you!"
We'll go through the process of creating and running the above mentioned file step by step:
- Create a file in the current folder by typing
touch hello-bash.sh
- Open the file with a text editor of your choice.
- Copy or retype the following three lines
#!/bin/bash
name="you"
echo Hello, $name
- First line in the example above indicates the script should run with bash shell
- In the second line we created a variable name and assigned it the value "you". Careful: Don't use space between a variable and its value.
- In the third line we used the command echo, which prints the string following the command to the output.
- Save the file.
- Make the file executable by typing
chmod u+x hello-bash.sh
- Execute the file by typing
./hello-bash.sh
You should see the output as in the example on the following image:
3. Conclusion
In this article we focused on the basics of Bash. We learned how to run basic shell commands, briefly mentioned pipelines and looked into writing Bash scripts. Bash is a very powerful language and if you'd like to learn more about what you can do with it, check out the Bash manual.