Skip to content
Ameen Abozaid
PublishedBEG-006-L07

Bash vs PowerShell cheat sheet

  • Lesson 7 of 8
  • 11 min read
  • Beginner
On This Lesson 11 topics

Lesson Overview

You have now met two shells that do the same jobs with different words. Bash says ls, and PowerShell says Get-ChildItem. Bash says export NAME=value, and PowerShell says $env:NAME = "value". Tutorials, teammates, and servers will use one or the other, and often you will meet the one you did not learn first.

In the picture of the literal assistant: you have two assistants who speak different languages and do the same chores. This lesson is the phrasebook. It gathers everything from the module into one table, explains why the two languages look so different, and gives you a method for finding the equivalent of any command, so you are never stuck.

This is a reference lesson, and it is short on new ideas. Its value is in connecting things you already know. Use it as a lookup page rather than something to memorize in one sitting; with practice the common ones will stick.

You need Lessons 1 to 6. Exact behavior can differ across versions and systems (Windows, macOS, Linux), so treat the table as a guide and confirm with your own help.

Learning Objectives

By the end of this lesson you will be able to:

  • Translate the module's main commands between Bash and PowerShell.
  • Explain what a PowerShell alias is, and why ls may work in both shells.
  • Find the PowerShell equivalent of a Bash command, and the reverse, using built-in tools.
  • Name the main syntax differences: variables, quoting, path separators, line continuation, and what a pipe carries.
  • Recognise the error a mixed-up command produces.
  • Choose one shell for a session and stay with it.

Written Explanation

Why they look so different

Bash comes from the Unix tradition: short command names (ls, cp, rm), options with dashes (-l, -r), and text flowing through pipes. PowerShell was designed by Microsoft with longer Verb-Noun names (Get-ChildItem, Copy-Item), named parameters (-Recurse), and objects flowing through pipes. Microsoft's documentation states that PowerShell pipelines pass objects from one command to the next.

Both are useful. Bash is the default language of most servers. PowerShell is the default of Windows and runs on macOS and Linux too.

The phrasebook

The table covers each job from this module. The PowerShell column gives the full command first and common aliases in brackets, when they exist on your system.

JobBashPowerShell
Show current folderpwdGet-Location (pwd)
Change foldercd pathSet-Location path (cd)
List folderls, ls -lGet-ChildItem (ls, dir)
Make foldermkdir name, mkdir -p a/bmkdir name or New-Item name -ItemType Directory
Make empty filetouch nameNew-Item name -ItemType File
Copy filecp a bCopy-Item a b (cp)
Copy foldercp -r a bCopy-Item a b -Recurse
Move or renamemv a bMove-Item a b (mv)
Delete filerm nameRemove-Item name (rm, del)
Delete folderrm -r nameRemove-Item name -Recurse
Show a filecat fileGet-Content file (cat)
Print textecho textWrite-Output text (echo)
Save outputcmd > filecmd > file
Append outputcmd >> filecmd >> file
Pipethe vertical bar between two commandsthe same vertical bar
Keep matching linesgrep wordSelect-String word
Sortsort, ls -lS (by size)Sort-Object, Sort-Object Length
Count lines or itemswc -lMeasure-Object
First N itemshead -n 5Select-Object -First 5
Read variableecho $NAME$env:NAME
Set variable (session)export NAME=value$env:NAME = "value"
List variablesprintenvGet-ChildItem Env:
Command historyhistoryGet-History (history)
Clear screenclearClear-Host (clear, cls)
Helpman cmd, cmd --helpGet-Help cmd
Make file runnablechmod +x fileno equivalent; see execution policy

Redirection and pipes look the same in both shells. What flows through is different (text versus objects), but the symbols are the same, which makes them an easy bridge.

Aliases: why ls works in PowerShell

An alias is a short nickname for a command. PowerShell ships with aliases that look like Unix commands, so ls, cd, cat, cp, and mv often work there. Microsoft's Remove-Item page lists the aliases del, erase, rd, and ri on all platforms, plus rm and rmdir on Windows.

Two traps follow:

  • An alias is not the real Bash command. ls -l may fail in PowerShell, because -l is a Bash option and PowerShell's Get-ChildItem does not have it. The alias name is borrowed, but the options are PowerShell's.
  • Aliases vary between systems. The Microsoft page says rm is an alias only on Windows. On PowerShell for Linux or macOS the real rm program is available instead. Learn the full names for anything you save in a script.

Finding an equivalent yourself

You do not need to memorize everything. Use the shell to ask:

  • PowerShell: Get-Alias ls shows what a nickname points to. Get-Command *item* lists commands with item in their name. Get-Help Copy-Item explains one.
  • Bash: type ls or which ls shows what a command is (output differs between systems), and man ls or ls --help explains it.
  • Search engine rule: search for the shell and the job, such as "powershell list files sorted by size". Include the shell's name, or the results will mix both worlds.

PowerShell's Verb-Noun pattern helps. If you know the verb you want (Get, Set, New, Remove, Copy, Move), tab completion after the dash shows the nouns.

Syntax differences to remember

TopicBashPowerShell
Path separator/\ on Windows; / is also accepted there
Variable read$NAME$env:NAME for environment variables
Spaces around =not allowed when settingallowed
Escape characterbackslash \backtick `
Line continuation\ at the end of the linea line ending in a pipe continues the pipeline; a backtick continues other lines
Comment# text# text
Options-r, --help-Recurse, named parameters
Case of namesfile names case-sensitive on Linuxcase-insensitive on Windows
PATH separator:; on Windows, : elsewhere

Microsoft's pipeline page confirms that a line ending in | continues the pipeline on the next line, and PowerShell 7 also accepts a pipe at the start of the next line. The PATH separators and case-sensitivity rules are confirmed on Microsoft's environment-variables page.

Do not mix them in one session

A shell understands one language. If you type a Bash command in PowerShell, or the reverse, you get an error that mentions the command not being recognised or found:

text
PS> export NAME=value
export : The term 'export' is not recognized as the name of a cmdlet...
$ Get-ChildItem
bash: Get-ChildItem: command not found

The wording varies with version. The remedy is to look at the prompt, decide which shell you are in, and use that shell's words. Some people run Bash tools inside PowerShell on Windows (through Git Bash or WSL) and are comfortable switching. For a beginner, one shell at a time is simpler.

Which one should you pick?

Pick the shell your computer provides by default (PowerShell on Windows, Bash or zsh on macOS and Linux), and learn the other's most common commands later using this page. If your career moves toward servers, expect Bash. If it moves toward Windows tooling, PowerShell will appear more often. Learning both at a basic level is a normal outcome.

Examples

Example 1: Translate a Bash line to PowerShell (simple)

Bash:

text
$ cp -r notes backup

Steps: the command cp becomes Copy-Item (or the alias cp), and the option -r becomes the named parameter -Recurse. PowerShell:

text
PS> Copy-Item notes backup -Recurse

Example 2: Translate a whole routine (practical)

A short Bash routine:

text
$ mkdir -p demo/src
$ touch demo/src/App.txt
$ ls demo/src > listing.txt
$ grep App listing.txt

The PowerShell version:

text
PS> mkdir demo/src
PS> New-Item demo/src/App.txt -ItemType File
PS> Get-ChildItem -Name demo/src > listing.txt
PS> Select-String App listing.txt

The listing line uses -Name so the file contains plain names, which Select-String can read. The fourth line prints the name of the file and the line number in front of the match, and grep does not. If you prefer plain output in PowerShell, pipe the file's text instead: Get-Content listing.txt | Select-String App.

Example 3: Diagnose a mixed-up command (realistic)

A colleague sends you the line export JAVA_OPTS="-Xmx512m" and says "it did not work". You are on Windows in PowerShell and it printed that export is not recognised.

Diagnosis: export is a Bash word. In PowerShell the same job is $env:JAVA_OPTS = "-Xmx512m". The value is the same; only the syntax changes. (The option itself is a Java setting you will meet later; here it is just example text.) The lesson: when an error says a command is not recognised, first ask "which shell is this line written for?"

Backend Insight

Backend teams usually run Linux servers, so deployment notes, container build files, and continuous-integration setups are most often written in Bash. Windows developers often work in PowerShell on their laptops, or run Bash through WSL or Git Bash so that they match the servers. Being able to read both makes you useful on mixed teams, and it makes copy-pasting from documentation much safer: you check the shell before running a line.

It also helps to remember that pipes carry text in Bash and objects in PowerShell. A script that parses text output may need to be rewritten, not just translated word for word, when it moves between them.

Common Mistakes

Mistake 1: Pasting a command written for the other shell

Check the prompt and the tutorial's context before pasting. If the error mentions a command not being recognised, suspect a shell mismatch.

Mistake 2: Assuming an alias accepts the same options

ls -l in PowerShell is Get-ChildItem -l, which is not valid. Use PowerShell's parameters, such as Get-ChildItem | Format-List, or learn the PowerShell way for each job.

Mistake 3: Trusting that aliases exist everywhere

Aliases such as rm and ls exist on Windows PowerShell but can differ on other systems. Use the full command name when you write something you want to keep.

Mistake 4: Searching without naming the shell

A search for "list files sorted by size" returns both worlds. Add "bash" or "powershell" to the search.

Exercises

Try each exercise yourself before opening the solutions.

Level A: Foundation

A1. Give the PowerShell command for rm -r old-logs.

Show solution A1

A1. Remove-Item old-logs -Recurse.

A2. Give the Bash command for Get-Content todo.txt.

Show solution A2

A2. cat todo.txt.

A3. Which symbols work the same in both shells: >, >>, |, export?

Show solution A3

A3. >, >>, and | work the same. export is Bash only.

Level B: Application

B1. Translate to PowerShell: mkdir -p app/config, then touch app/config/settings.txt, then cat app/config/settings.txt.

Show solution B1

B1. mkdir app/config (or New-Item app/config -ItemType Directory), New-Item app/config/settings.txt -ItemType File, Get-Content app/config/settings.txt.

B2. Translate to Bash: $env:MODE = "test", then $env:MODE, then Get-ChildItem Env: | Select-String MODE.

Show solution B2

B2. export MODE=test, echo $MODE, printenv | grep MODE.

B3. In your own shell, use Get-Alias (PowerShell) or type (Bash) to find out what one command you already use really is. Write down the command and the answer.

Show solution B3

B3. Answer depends on your shell. Example: Get-Alias ls shows that ls points to Get-ChildItem; type ls in Bash shows where the command comes from.

Level C: Challenge

C1. You find this in a tutorial: ls -lS | head -3 > top3.txt. Write a PowerShell version, and say one difference between the two outputs.

Show solution C1

C1. Get-ChildItem | Sort-Object Length -Descending | Select-Object -First 3 > top3.txt. Difference: the Bash ls -lS output includes a "total" line on many systems, and the PowerShell output is a formatted table of objects that may also include folders.

C2. A teammate says: "PowerShell and Bash are the same, just with different spellings." Explain in three sentences why this is only partly true, using what flows through a pipe.

Show solution C2

C2. Both shells connect commands with the same | symbol, but Bash passes plain text between commands, and PowerShell passes objects that have named properties such as Length. That means sorting by size in PowerShell uses a property name while Bash relies on an option of ls. So the commands look alike on the surface, but a script cannot always be translated word for word.

Mini Challenge

Make your own one-page cheat card. List ten commands you used in this module, with the Bash and PowerShell version side by side. Run each version you can on your own computer and mark it checked. For any you cannot run, mark it unchecked and explain why (wrong shell, missing tool, system difference).

Check your work: every row has both columns, at least one row uses a pipe, and at least one row uses an environment variable.

Summary & Next Lesson

Key takeaways

  • Bash and PowerShell do the same jobs with different words: short names and text pipes in Bash, Verb-Noun names and object pipes in PowerShell.
  • >, >>, and | look the same in both.
  • PowerShell aliases borrow Unix names, but options differ, and aliases can vary by system.
  • To find an equivalent, ask the shell: Get-Alias, Get-Command, Get-Help, type, man.
  • Check the prompt before pasting a command. Do not mix the two languages in one session.

Next lesson

You now have the phrasebook. The next lesson, "Demo: setting up a project folder using only the terminal", puts the whole module together in one hands-on walk-through.

Self-check

  1. What is the PowerShell command for cat file?
  2. Why can ls work in PowerShell?
  3. What does Bash use to set a session variable? What does PowerShell use?
  4. What is the main difference in what a pipe carries?
  5. What should you check before pasting a command from a tutorial?
Show answers
  1. Get-Content file.
  2. PowerShell has an alias named ls that points to Get-ChildItem.
  3. Bash: export NAME=value. PowerShell: $env:NAME = "value".
  4. Bash pipes carry text. PowerShell pipes carry objects.
  5. Which shell the command is written for, and whether it matches your prompt.

References

  • Microsoft Learn, Remove-Item. Supports: the alias list (del, erase, rd, ri on all platforms; rm and rmdir on Windows only), the -Recurse parameter, and the Verb-Noun naming style.
  • Microsoft Learn, about_Pipelines. Supports: pipelines pass objects, the Sort-Object, Where-Object, and Measure-Object patterns, and pipeline line continuation (a trailing |, and in PowerShell 7 a leading |).
  • Microsoft Learn, about_Redirection. Supports: > and >> in PowerShell.
  • Microsoft Learn, about_Environment_Variables. Supports: $Env:NAME syntax, Get-ChildItem Env:, PATH separators, and case sensitivity differences between Windows and other systems.
  • Not verified on an official page: the other aliases in the table (cd, cat, cp, mv, echo, history, clear, cls, dir), Get-Alias, Get-Command, type, which, the Bash backslash line continuation, the backtick escape, and exact error messages. These are standard but were not checked in this session.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.