mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-06 06:39:18 -05:00
feat(curriculum): add understanding the command line and working with bash lectures (#61430)
Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
@@ -4452,6 +4452,12 @@
|
||||
"title": "Python Review",
|
||||
"intro": ["Review Python concepts to prepare for the upcoming exam."]
|
||||
},
|
||||
"lecture-understanding-the-command-line-and-working-with-bash": {
|
||||
"title": "Understanding the Command Line and Working with Bash",
|
||||
"intro": [
|
||||
"Learn about the Command Line and Working with Bash in these lectures."
|
||||
]
|
||||
},
|
||||
"workshop-bash-boilerplate": {
|
||||
"title": "Build a Boilerplate",
|
||||
"intro": [
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to Understanding the Command Line and Working with Bash
|
||||
block: lecture-understanding-the-command-line-and-working-with-bash
|
||||
superBlock: full-stack-developer
|
||||
---
|
||||
|
||||
## Introduction to Understanding the Command Line and Working with Bash
|
||||
|
||||
Learn about Understanding the Command Line and Working with Bash in these lectures.
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "Understanding the Command Line and Working with Bash",
|
||||
"blockType": "lecture",
|
||||
"blockLayout": "challenge-list",
|
||||
"isUpcomingChange": true,
|
||||
"dashedName": "lecture-understanding-the-command-line-and-working-with-bash",
|
||||
"superBlock": "full-stack-developer",
|
||||
"helpCategory": "Backend Development",
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "687e910842f3322350bc8d93",
|
||||
"title": "What Is the Terminal, and How Does It Differ from the Command Line?"
|
||||
},
|
||||
{
|
||||
"id": "687e9437536d762823fefab3",
|
||||
"title": "What Are Some Shortcuts You Can Use in the Command Line to Speed Up Productivity?"
|
||||
},
|
||||
{
|
||||
"id": "687e944b821130283dc83b2e",
|
||||
"title": "What Is Bash, and What Are Some Basic Commands?"
|
||||
},
|
||||
{
|
||||
"id": "687e947ae3ea7d2867c3d0e1",
|
||||
"title": "What Are Some Command Options and Flags?"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
---
|
||||
id: 687e910842f3322350bc8d93
|
||||
title: What Is the Terminal, and How Does It Differ from the Command Line?
|
||||
challengeType: 19
|
||||
dashedName: what-is-the-terminal-and-how-does-it-differ-from-the-command-line
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Let's learn about the terminal, and how it differs from the command line.
|
||||
|
||||
The command line is a basic text input interface which allows a user to enter "commands", usually in the form of a series of characters, and submit or execute them, usually by pressing the "Enter" key. You will most-commonly see command line interfaces within a terminal.
|
||||
|
||||
A terminal is a special application that offers a command line interface to perform system-level commands beyond the basic read/write operations. You may also hear about "terminal emulators", which are essentially applications that wrap a basic terminal interface to offer additional features or functionality. But for most general purposes, you are probably safe to refer to these as "terminals" as well.
|
||||
|
||||
Finally, we will also need to talk about "shells". A shell is the software that wraps the command line and interprets your inputs as commands, returning the output.
|
||||
|
||||
But how can you access the terminal on your system?
|
||||
|
||||
Well, if you are running a modern version of Windows, you will likely have two different options. Windows offers two distinct shell applications out of the box: Powershell and Command Prompt.
|
||||
|
||||
Both of these shells come with their own terminal applications (which share the name of the respective shell), so you can use your application menu to start either a Powershell terminal or a Command Prompt terminal.
|
||||
|
||||
Windows also offers a relatively new and modernized terminal emulator aptly named Microsoft Terminal.
|
||||
|
||||
You can install this application from the Windows Store, and are then able to launch it just like you would your Powershell or Command Prompt terminals. The advantage of the Microsoft Terminal is that it allows you to access both the Powershell and Command Prompt shells from the same application, as well as your respective Linux shell if you have configured Windows Subsystems for Linux.
|
||||
|
||||
MacOS offers a terminal that is aptly named "Terminal". You can access this through your spotlight search, or through your application launcher.
|
||||
|
||||
MacOS also offers support for third-party terminal emulators, such as iTerm or Ghostty. You can install these applications from their home pages.
|
||||
|
||||
On Linux, the default terminal (and how you launch it) depends highly on the distro and desktop environment you use. There's a lot of support for third-party terminal emulators, too. For example, you could set up kitty in Arch Linux - and you can launch it with an application manager such as wofi.
|
||||
|
||||
Linux arguably has the largest variety of terminal applications to choose from. Most of these can be installed through your distro's package manager.
|
||||
|
||||
The distinction between "terminal", "shell", and "command line" may seem rather pedantic at times. And for the most part, the terms can generally be used interchangeably. But it is important to know that they have specific meanings, and what each of those meanings are.
|
||||
|
||||
# --questions--
|
||||
|
||||
## --text--
|
||||
|
||||
What is a terminal?
|
||||
|
||||
## --answers--
|
||||
|
||||
A basic text input interface for entering commands.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about how the lecture distinguishes between different components of command-line interfaces.
|
||||
|
||||
---
|
||||
|
||||
A special application that offers a command line interface for system-level commands.
|
||||
|
||||
---
|
||||
|
||||
Software that removes all user inputs in the command line.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about how the lecture distinguishes between different components of command-line interfaces.
|
||||
|
||||
---
|
||||
|
||||
The Enter key used to submit commands.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about how the lecture distinguishes between different components of command-line interfaces.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
2
|
||||
|
||||
## --text--
|
||||
|
||||
Which of the following best describes a shell?
|
||||
|
||||
## --answers--
|
||||
|
||||
A hardware component that displays text input.
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture explains what interprets your commands when you type them.
|
||||
|
||||
---
|
||||
|
||||
A special application that allows system-level commands.
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture explains what interprets your commands when you type them.
|
||||
|
||||
---
|
||||
|
||||
Software that wraps the command line and interprets inputs as commands.
|
||||
|
||||
---
|
||||
|
||||
A third-party terminal emulator.
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture explains what interprets your commands when you type them.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
3
|
||||
|
||||
## --text--
|
||||
|
||||
On Windows, what advantage does Microsoft Terminal offer over traditional options?
|
||||
|
||||
## --answers--
|
||||
|
||||
It's the only way to access the command line on Windows.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Consider what functionality Microsoft Terminal provides that the traditional Windows terminals don't.
|
||||
|
||||
---
|
||||
|
||||
It allows access to both Powershell and Command Prompt shells from the same application.
|
||||
|
||||
---
|
||||
|
||||
It's the default terminal on all Windows systems.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Consider what functionality Microsoft Terminal provides that the traditional Windows terminals don't.
|
||||
|
||||
---
|
||||
|
||||
It replaces the need for a shell entirely.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Consider what functionality Microsoft Terminal provides that the traditional Windows terminals don't.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
2
|
||||
@@ -0,0 +1,138 @@
|
||||
---
|
||||
id: 687e9437536d762823fefab3
|
||||
title: What Are Some Shortcuts You Can Use in the Command Line to Speed Up Productivity?
|
||||
challengeType: 19
|
||||
dashedName: what-are-some-shortcuts-you-can-use-in-the-command-line-to-speed-up-productivity
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Let's learn about command line shortcuts you can use to speed up productivity.
|
||||
|
||||
Terminal and command line shortcuts can help you streamline your workflows and maximize your productivity. For Linux and macOS, which can both trace their roots to Unix, many of these shortcuts will be the same. But for Windows, there will be some differences.
|
||||
|
||||
The first shortcut is the up arrow key. Pressing this key will allow you to cycle backwards through your command history, one command at a time. The down arrow key, then, can be used to cycle forwards through your command history. These two keys allow you to quickly cycle through the commands you have previously run to find one you need to run again.
|
||||
|
||||
Many shells will also offer an auto-complete feature, proposing commands based on what you have started to type. The tab key can be used to fill in the rest of the suggestion, quickly populating your command line with the full syntax. How these suggestions are generated will vary from shell to shell. For example, in zsh these suggestions are based on your most recent command history. But in PowerShell, they are based on commands, variables, and arguments that are available (with less weight on how recently they were used).
|
||||
|
||||
Sometimes a command may result in a lot of output on your terminal. This extra noise can get in the way, or become disruptive to your workflow. In *nix based terminals found on Linux and macOS devices, you can press Control + L to clear the entire screen and start with a fresh clean prompt. In PowerShell, you would need to run the `cls` command (which you can bind to a key combination like Control + L).
|
||||
|
||||
If you need to interrupt a command to stop its execution, you can use Control + C to kill the process. This will terminate the command and create a new prompt, allowing you to continue with whatever else you may need to work on. For PowerShell users, Control + C is also used to copy text - and will only work to terminate a process when the context is not ambiguous (such as when there is no text selected to copy).
|
||||
|
||||
The next couple of shortcuts are only available in *nix based terminals, and do not have a PowerShell alternative.
|
||||
|
||||
There may be times when you need to multitask, allowing a process or command to run in the background while you work on another. Pressing Control + Z places the current process in a background task and returns you to the command line, where you can continue your work. When you need to shift focus back to the background task, you can use `fg` to restore it.
|
||||
|
||||
And finally, there may be commands you need to run twice in a row. You can press the up arrow key to cycle backwards to the previously run command, but you can also type two exclamation points (`!!`) into the command line and hit enter - this will automatically run the last executed command.
|
||||
|
||||
Many terminals offer even more shortcuts, and I encourage you to read the documentation for your particular setup to find the shortcuts that work best for you. But these basic universal shortcuts should serve as a great starting point for maximizing your productivity and becoming a terminal wizard.
|
||||
|
||||
# --questions--
|
||||
|
||||
## --text--
|
||||
|
||||
Which key combination is used to clear the screen in *nix based terminals?
|
||||
|
||||
## --answers--
|
||||
|
||||
Control + C
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about which shortcut creates a fresh, clean prompt.
|
||||
|
||||
---
|
||||
|
||||
Control + L
|
||||
|
||||
---
|
||||
|
||||
Control + Z
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about which shortcut creates a fresh, clean prompt.
|
||||
|
||||
---
|
||||
|
||||
The tab key
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about which shortcut creates a fresh, clean prompt.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
2
|
||||
|
||||
## --text--
|
||||
|
||||
What happens when you press Control + Z in a *nix based terminal?
|
||||
|
||||
## --answers--
|
||||
|
||||
It terminates the current process.
|
||||
|
||||
### --feedback--
|
||||
|
||||
This shortcut allows you to multitask while a process runs.
|
||||
|
||||
---
|
||||
|
||||
It places the current process in the background.
|
||||
|
||||
---
|
||||
|
||||
It clears the screen.
|
||||
|
||||
### --feedback--
|
||||
|
||||
This shortcut allows you to multitask while a process runs.
|
||||
|
||||
---
|
||||
|
||||
It cycles through command history.
|
||||
|
||||
### --feedback--
|
||||
|
||||
This shortcut allows you to multitask while a process runs.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
2
|
||||
|
||||
## --text--
|
||||
|
||||
Which of the following can be used to quickly run the last executed command in a *nix based terminal?
|
||||
|
||||
## --answers--
|
||||
|
||||
Control + L
|
||||
|
||||
### --feedback--
|
||||
|
||||
This involves typing a specific character twice.
|
||||
|
||||
---
|
||||
|
||||
The tab key
|
||||
|
||||
### --feedback--
|
||||
|
||||
This involves typing a specific character twice.
|
||||
|
||||
---
|
||||
|
||||
`!!`
|
||||
|
||||
---
|
||||
|
||||
Control + C
|
||||
|
||||
### --feedback--
|
||||
|
||||
This involves typing a specific character twice.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
3
|
||||
@@ -0,0 +1,142 @@
|
||||
---
|
||||
id: 687e944b821130283dc83b2e
|
||||
title: What Is Bash, and What Are Some Basic Commands?
|
||||
challengeType: 19
|
||||
dashedName: what-is-bash-and-what-are-some-bash-commands
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Let's learn about Bash and some of its basic commands.
|
||||
|
||||
Bash stands for Bourne Again SHell, and is arguably the most common shell you will encounter in Unix-like environments. As such, it can be very beneficial to understand how to navigate this shell and use the common commands.
|
||||
|
||||
The first command is `pwd`, which prints the current working directory to the terminal. The "working directory" refers to the directory the terminal is currently pointed at, which is an important thing to know for many of these other commands.
|
||||
|
||||
It is entirely possible that your terminal is pointed to the wrong directory. The `cd` command allows you to change directories. You can specify an absolute path, prefixed with a forward slash (`/`), a relative path with no prefix, and use the double-dot (`..`) syntax to move up to the parent directory.
|
||||
|
||||
The next command is `ls`, which lists the contents of your current working directory. This is helpful to see if a file or folder exists in your directory. The `ls` command also takes flags to show hidden files (the `-a` flag), file permissions (the `-l` flag), and other features. You will learn more about command flags in the next lecture. You can also use `less` or `cat` to view the contents of a file.
|
||||
|
||||
What if the directory does not contain the file or folder you need? There are commands called `mkdir` and touch that allow you to create them. `mkdir` creates a new directory, or folder, and `touch` creates a new file. Both of these commands accept the name of the file or folder you want to create. For example, `touch readme.md` creates a new Markdown file named `readme.md`.
|
||||
|
||||
Maybe your new file should not have been named `readme.md`, but it should have been `readthis.md`. Thankfully, the `mv` command allows you to move (or rename) a file. It takes the old file name and the new file name.
|
||||
|
||||
The `rm` command allows you to remove (or delete) a file, and with the `-r` flag will delete a directory. Sometimes a file might be protected, and you'll need to include the `-f` flag.
|
||||
|
||||
The `cp` command can be used to copy a file or directory to a new location. Unlike the `mv` command, the `cp` command does not remove the original file - and in order to copy a directory, you'll need to pass the `-r` flag.
|
||||
|
||||
The `echo` command can be thought of as the Bash equivalent of a `console.log()` or `print()` function. `echo` takes a string argument, wrapped in quotes, and prints it to the terminal. This might seem silly to run in the terminal, just to get text back in the terminal, but you can actually chain `echo` to a control symbol. The `>` symbol allows you to specify a filename to create or overwrite with the new string, like `echo "Naomi was here." > readme.md`, and the `>>` symbol will append to the file.
|
||||
|
||||
There are many more Bash commands available to you, such as `head`, `tail`, `chown`, and `chmod`. It would be impossible to cover them all in this lecture. But you can use the man command to see the manual or help page for nearly any command.
|
||||
|
||||
You'll likely remember the commands you use most frequently. And for the rest, you can always check the man page.
|
||||
|
||||
# --questions--
|
||||
|
||||
## --text--
|
||||
|
||||
What does the `pwd` command do in Bash?
|
||||
|
||||
## --answers--
|
||||
|
||||
Changes directories.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about what information this command provides to help you navigate.
|
||||
|
||||
---
|
||||
|
||||
Prints the current working directory.
|
||||
|
||||
---
|
||||
|
||||
Creates a new file.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about what information this command provides to help you navigate.
|
||||
|
||||
---
|
||||
|
||||
Lists directory contents.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about what information this command provides to help you navigate.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
2
|
||||
|
||||
## --text--
|
||||
|
||||
Which command would you use to create a new file called `notes.txt`?
|
||||
|
||||
## --answers--
|
||||
|
||||
`mkdir notes.txt`
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture mentions a specific command for creating new files.
|
||||
|
||||
---
|
||||
|
||||
`cd notes.txt`
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture mentions a specific command for creating new files.
|
||||
|
||||
---
|
||||
|
||||
`touch notes.txt`
|
||||
|
||||
---
|
||||
|
||||
`ls notes.txt`
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture mentions a specific command for creating new files.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
3
|
||||
|
||||
## --text--
|
||||
|
||||
If you wanted to rename a file from `oldname.txt` to `newname.txt`, which command would you use?
|
||||
|
||||
## --answers--
|
||||
|
||||
`rm oldname.txt newname.txt`
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture mentions that this command can be used for both moving and renaming files.
|
||||
|
||||
---
|
||||
|
||||
`mv oldname.txt newname.txt`
|
||||
|
||||
---
|
||||
|
||||
`touch newname.txt`
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture mentions that this command can be used for both moving and renaming files.
|
||||
|
||||
---
|
||||
|
||||
`cd oldname.txt newname.txt`
|
||||
|
||||
### --feedback--
|
||||
|
||||
The lecture mentions that this command can be used for both moving and renaming files.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
2
|
||||
@@ -0,0 +1,154 @@
|
||||
---
|
||||
id: 687e947ae3ea7d2867c3d0e1
|
||||
title: What Are Some Command Options and Flags?
|
||||
challengeType: 19
|
||||
dashedName: what-are-some-command-options-and-flags
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Let's learn about command options and flags.
|
||||
|
||||
In a previous lecture, you learned about passing arguments to commands such as `touch readme.md`. Options, or flags, are special arguments you can pass to a command that affect the way it behaves. The two terms are used interchangeably, though "flags" tends to be used more specifically for options that serve as an on/off toggle.
|
||||
|
||||
Options are typically prefixed with one or two hyphen (`-`) symbols, which helps provide a visual distinction between an option and an argument. First, let's look at the two-hyphen, or "long form", syntax.
|
||||
|
||||
Many applications, such as `ls`, accept a `--version` flag, which prints the current version of the application to the terminal instead of running the application's commands.
|
||||
|
||||
Many applications also offer a `--help` flag, which prints instructions on how to use the application.
|
||||
|
||||
The one-hyphen, or "short form", syntax typically uses options that are a single letter. For example, the `-a` flag with `ls` lists all files, including hidden files that start with a dot (`.`), like `.env`.
|
||||
|
||||
The advantage of these short options is that you can chain multiple flags together. Instead of `ls --all --human-readable --size` you can use the single letters all at once with `ls -ahs`.
|
||||
|
||||
Some options expect a value to be passed to them.
|
||||
|
||||
When using long-form options, you typically need to use an equal sign. In this syntax, the value is directly concatenated to the option with the equal (`=`) symbol. Here is an example of modifying the behavior of `ls` to either include or exclude colors:
|
||||
|
||||
```bash
|
||||
ls --color=never
|
||||
```
|
||||
|
||||
When using short-form options, you typically separate the value with a space. For example, here are the long and short form options for setting the `width` of the `ls` result:
|
||||
|
||||
```bash
|
||||
ls -w 50
|
||||
```
|
||||
|
||||
```bash
|
||||
ls --width=50
|
||||
```
|
||||
|
||||
Notice how the short form uses `-w 50`, while the long form uses `--width=50`. This distinction is important to know, to avoid passing an option value as a positional argument instead.
|
||||
|
||||
But if you are ever unsure, remember you can usually use the `--help` flag to see the expected syntax for options!
|
||||
|
||||
# --questions--
|
||||
|
||||
## --text--
|
||||
|
||||
What is the difference between "long form" and "short form" syntax for command options?
|
||||
|
||||
## --answers--
|
||||
|
||||
Long form uses a single hyphen while short form uses two hyphens.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about the number of hyphens used in each form.
|
||||
|
||||
---
|
||||
|
||||
Long form uses two hyphens while short form uses a single hyphen.
|
||||
|
||||
---
|
||||
|
||||
Long form is only used for version information.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about the number of hyphens used in each form.
|
||||
|
||||
---
|
||||
|
||||
There is no difference between the two forms.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Think about the number of hyphens used in each form.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
2
|
||||
|
||||
## --text--
|
||||
|
||||
What is the advantage of using short form options like `-ahs`?
|
||||
|
||||
## --answers--
|
||||
|
||||
They require less memory to process.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Consider how multiple flags can be combined in short form.
|
||||
|
||||
---
|
||||
|
||||
They are easier to remember.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Consider how multiple flags can be combined in short form.
|
||||
|
||||
---
|
||||
|
||||
Multiple options can be chained together.
|
||||
|
||||
---
|
||||
|
||||
They always work better than long form options.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Consider how multiple flags can be combined in short form.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
3
|
||||
|
||||
## --text--
|
||||
|
||||
When using a long-form option that requires a value, what syntax is typically used?
|
||||
|
||||
## --answers--
|
||||
|
||||
`--option value`
|
||||
|
||||
### --feedback--
|
||||
|
||||
Remember the example of setting `width` with the long form option.
|
||||
|
||||
---
|
||||
|
||||
`--option=value`
|
||||
|
||||
---
|
||||
|
||||
`-o value`
|
||||
|
||||
### --feedback--
|
||||
|
||||
Remember the example of setting `width` with the long form option.
|
||||
|
||||
---
|
||||
|
||||
`-o=value`
|
||||
|
||||
### --feedback--
|
||||
|
||||
Remember the example of setting `width` with the long form option.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
2
|
||||
@@ -1385,6 +1385,9 @@
|
||||
"dashedName": "bash-fundamentals",
|
||||
"comingSoon": true,
|
||||
"blocks": [
|
||||
{
|
||||
"dashedName": "lecture-understanding-the-command-line-and-working-with-bash"
|
||||
},
|
||||
{
|
||||
"dashedName": "workshop-bash-boilerplate"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user