ورقة مرجعية: Bash مقابل PowerShell
في هذا الدرس 11 موضوعات
هذا الدرس متاح حاليًا باللغة الإنجليزية فقط، ولم تُنشر نسخته العربية بعد.
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
lsmay 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.
| Job | Bash | PowerShell |
|---|---|---|
| Show current folder | pwd | Get-Location (pwd) |
| Change folder | cd path | Set-Location path (cd) |
| List folder | ls, ls -l | Get-ChildItem (ls, dir) |
| Make folder | mkdir name, mkdir -p a/b | mkdir name or New-Item name -ItemType Directory |
| Make empty file | touch name | New-Item name -ItemType File |
| Copy file | cp a b | Copy-Item a b (cp) |
| Copy folder | cp -r a b | Copy-Item a b -Recurse |
| Move or rename | mv a b | Move-Item a b (mv) |
| Delete file | rm name | Remove-Item name (rm, del) |
| Delete folder | rm -r name | Remove-Item name -Recurse |
| Show a file | cat file | Get-Content file (cat) |
| Print text | echo text | Write-Output text (echo) |
| Save output | cmd > file | cmd > file |
| Append output | cmd >> file | cmd >> file |
| Pipe | the vertical bar between two commands | the same vertical bar |
| Keep matching lines | grep word | Select-String word |
| Sort | sort, ls -lS (by size) | Sort-Object, Sort-Object Length |
| Count lines or items | wc -l | Measure-Object |
| First N items | head -n 5 | Select-Object -First 5 |
| Read variable | echo $NAME | $env:NAME |
| Set variable (session) | export NAME=value | $env:NAME = "value" |
| List variables | printenv | Get-ChildItem Env: |
| Command history | history | Get-History (history) |
| Clear screen | clear | Clear-Host (clear, cls) |
| Help | man cmd, cmd --help | Get-Help cmd |
| Make file runnable | chmod +x file | no 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 -lmay fail in PowerShell, because-lis a Bash option and PowerShell'sGet-ChildItemdoes not have it. The alias name is borrowed, but the options are PowerShell's. - Aliases vary between systems. The Microsoft page says
rmis an alias only on Windows. On PowerShell for Linux or macOS the realrmprogram 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 lsshows what a nickname points to.Get-Command *item*lists commands withitemin their name.Get-Help Copy-Itemexplains one. - Bash:
type lsorwhich lsshows what a command is (output differs between systems), andman lsorls --helpexplains 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
| Topic | Bash | PowerShell |
|---|---|---|
| Path separator | / | \ on Windows; / is also accepted there |
| Variable read | $NAME | $env:NAME for environment variables |
Spaces around = | not allowed when setting | allowed |
| Escape character | backslash \ | backtick ` |
| Line continuation | \ at the end of the line | a line ending in a pipe continues the pipeline; a backtick continues other lines |
| Comment | # text | # text |
| Options | -r, --help | -Recurse, named parameters |
| Case of names | file names case-sensitive on Linux | case-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:
PS> export NAME=value
export : The term 'export' is not recognized as the name of a cmdlet...
$ Get-ChildItem
bash: Get-ChildItem: command not foundThe 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:
$ cp -r notes backupSteps: the command cp becomes Copy-Item (or the alias cp), and the option -r becomes the named parameter -Recurse. PowerShell:
PS> Copy-Item notes backup -RecurseExample 2: Translate a whole routine (practical)
A short Bash routine:
$ mkdir -p demo/src
$ touch demo/src/App.txt
$ ls demo/src > listing.txt
$ grep App listing.txtThe PowerShell version:
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.txtThe 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.
أظهر الحل A1
A1. Remove-Item old-logs -Recurse.
A2. Give the Bash command for Get-Content todo.txt.
أظهر الحل A2
A2. cat todo.txt.
A3. Which symbols work the same in both shells: >, >>, |, export?
أظهر الحل 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.
أظهر الحل 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.
أظهر الحل 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.
أظهر الحل 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.
أظهر الحل 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.
أظهر الحل 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
- What is the PowerShell command for
cat file? - Why can
lswork in PowerShell? - What does Bash use to set a session variable? What does PowerShell use?
- What is the main difference in what a pipe carries?
- What should you check before pasting a command from a tutorial?
أظهر الإجابات
Get-Content file.- PowerShell has an alias named
lsthat points toGet-ChildItem. - Bash:
export NAME=value. PowerShell:$env:NAME = "value". - Bash pipes carry text. PowerShell pipes carry objects.
- 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,rion all platforms;rmandrmdiron Windows only), the-Recurseparameter, and the Verb-Noun naming style. - Microsoft Learn, about_Pipelines. Supports: pipelines pass objects, the
Sort-Object,Where-Object, andMeasure-Objectpatterns, 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:NAMEsyntax,Get-ChildItem Env:,PATHseparators, 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.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.