/writing

A beginner’s guide to Bash & Zsh

A beginner’s guide to Bash & Zsh

The command line interface, with its powerful shell environments like Bash and Zsh, allows you to interact with your computer using text-based commands. On opening up your terminal, you’ll see a prompt where you can start typing commands. Assuming your default shell is either Bash or Zsh, this tutorial should work just fine.

This guide is not intended to be an exhaustive resource on shell configuration or advanced command usage. Instead, we will focus on laying a basic foundation that will enable you to navigate the command line and get started.

What is Bash?

Bash, short for "Bourne Again SHell," is a popular command-line shell and scripting language used in Unix-based systems like Linux and macOS. It provides a text-based interface for interacting with the operating system and executing various commands.

What is Zsh?

Zsh, short for "Z Shell," is another powerful command-line shell available as an alternative to Bash. It offers additional features and customization options, making it a popular choice for many users.

Getting Started with Bash or Zsh

To begin, open your terminal. You can find it in your applications or use a keyboard shortcut like Ctrl + Alt + T on most Linux distributions. On macOS, you can find it in the "Utilities" folder within the "Applications" directory.

Once the terminal is open, you will see a prompt where you can start entering commands. This is where it all begins!

Homer GIF

Basic Commands

Let’s start with some basic commands that you can try out in your terminal:

  • pwd (Print Working Directory): This command displays the current directory you are in. For example, if you are in the home directory, running pwd will output /home/username.

  • ls (List): Use this command to list the files and directories in the current directory. For example, running ls will display a list of files and directories in the current directory.

  • cd (Change Directory): This command allows you to navigate to a different directory. For example, running cd Documents will change your current directory to the "Documents" directory.

  • mkdir (Make Directory): Use this command to create a new directory. For example, running mkdir myfolder will create a new directory named "myfolder" in the current directory.

  • touch (Create File): This command creates a new file. For example, running touch myfile.txt will create a new empty file named "myfile.txt" in the current directory.

Remember to press Enter after typing each command to execute it.

Command Options and Arguments

Commands often come with options and arguments that modify their behavior. Options are typically specified using a hyphen followed by a letter or word, such as -l or --help. Arguments are additional information provided to a command, such as a file or directory name.

For example:

  • ls -l: Lists files in the long format. Here -l is the option being specified. Running ls -l will display detailed information about each file in the current directory, including permissions, owner, size, and modification date.

  • mkdir myfolder: Creates a directory named "myfolder". Here myfolder is the argument. Running this command will create a new directory named "myfolder" in the current directory.

Command History

Bash and Zsh keep track of the commands you’ve previously entered, allowing you to access them easily. You can use the up and down arrow keys to navigate through your command history. Press Enter to execute a previously entered command.

Tab Completion

Tab completion is a helpful feature that saves you from typing out long file or directory names. When you start typing, press the Tab key, and the shell will try to auto-complete the rest of the name for you. If there are multiple possibilities, pressing Tab twice will show you the available options.

For better tab completions, auto-complete, and syntax highlighting you can configure your .zshrc file. I have a github repository with sensible defaults (in my opinion) and all the necessary configuration for a better experience.

If you want to use a package instead you can look into ‘Oh My Zsh’.

Scripting with Bash or Zsh

Bash and Zsh can also be used as scripting languages, allowing you to automate tasks and create more complex programs. Shell scripts are plain text files with a series of commands that can be executed together.

To create a script, open a text editor and save the file with a .sh extension (e.g., myscript.sh). Make the file executable by running the command chmod +x myscript.sh. Now, you can run the script by typing ./myscript.sh in the terminal.

More

Here are some articles that I found useful when I started out with bash:

Final Thoughts

While there is much more to learn about Bash and Zsh, this guide should provide you with a solid foundation to start exploring and using the command line effectively. As you gain more experience, you can delve into advanced topics and configurations to enhance your command-line experience even further.

Note: The commands and examples provided in this article are primarily targeted at Unix-based systems like Linux and macOS. Some commands or options may differ on other operating systems.

Written with Mavié.

§

If you enjoyed reading this, subscribe to my newsletter!