Ironman Software Forums
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
In this blog post, we’ll look at examples of bash scripts and how to do the same thing in PowerShell.
Many of the bash commands found in this post where sourced from LinuxHint. If you have more comparisons you’d like to see, please contact us.
We also have a Python vs PowerShell Cheat Sheet.
Write text to the screen.
Bash
echo "Hello, World!"
echo -e "Hello, \t World!"
PowerShell
Write-Output 'Hello, World!'
Write-Output "Hello, `t World!"
# Alias is also echo
echo 'Hello, World!'
Save and execute a script with bash and PowerShell.
Bash
Assume that echo1.sh
has the following contents.
#!/bin/bash
echo "Hello, World!"
You would the execute it with the following command line.
bash echo1.sh
PowerShell
Assume echo1.ps1
has the following contents.
#!/usr/bin/env pwsh
Write-Host "Hello, World!"
You would then execute it with the following command line.
pwsh echo1.ps1
Comments are the same in PowerShell and bash.
Bash
# My Comment!
PowerShell
# My Comment!
Multiline comments are different between bash and PowerShell.
Bash
#!/bin/bash
: 'Multi
line
comment'
echo "42"
PowerShell
#!/usr/bin/env pwsh
<#
Multi
line
comment
#>
Write-Host "42"
Bash
#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done
PowerShell
#!/usr/bin/env pwsh
$count = 0
while($true) {
Write-Host $count
if ($count -eq 5) {
break
}
$count++
}
Bash
#!/bin/bash
for (( counter=10; counter>0; counter-- ))
do
echo -n "$counter "
done
PowerShell
#!/usr/bin/env pwsh
for($counter = 10; $counter -gt 0; $counter--) {
Write-Host $counter
}
Bash
#!/bin/bash
echo "Enter Your Name"
read name
echo "Welcome, $name!"
PowerShell
#!/usr/bin/env pwsh
$name = Read-Host "Enter Your Name"
Write-Host "Welcome, $name!"
Bash
#!/bin/bash
n=10
if [ $n -lt 10 ];
then
echo "It is a one digit number"
else
echo "It is a two digit number"
fi
PowerShell
#!/usr/bin/env pwsh
$n = 10
if ($n -lt 10) {
Write-Host "It is a one digit number"
} else {
Write-Host "It is a two digit number"
}
Bash
#!/bin/bash
echo "Enter username"
read username
echo "Enter password"
read password
if [[ ( $username == "admin" && $password == "secret" ) ]]; then
echo "valid user"
else
echo "invalid user"
fi
PowerShell
#!/usr/bin/env pwsh
$UserName = Read-Host "Enter username"
$Password = Read-Host "Enter password"
if ($username -eq 'admin' -and $password -eq 'secret') {
Write-Host 'valid user'
} else {
Write-Host 'invalid user'
}
Bash
#!/bin/bash
echo "Enter any number"
read n
if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the game"
else
echo "You lost the game"
fi
PowerShell
#!/usr/bin/env pwsh
[int]$n = Read-Host "Enter any number"
if ($n -eq 15 -or $n -eq 45) {
Write-Host "You won the game"
} else {
Write-Host "You lost the game"
}
Bash
#!/bin/bash
echo "Enter your lucky number"
read n
if [ $n -eq 101 ];
then
echo "You got 1st prize"
elif [ $n -eq 510 ];
then
echo "You got 2nd prize"
elif [ $n -eq 999 ];
then
echo "You got 3rd prize"
else
echo "Sorry, try for the next time"
fi
PowerShell
#!/usr/bin/env pwsh
[int]$n = Read-Host "Enter your lucky number"
if ($n -eq 101) {
Write-Host "You got 1st prize"
} elseif ($n -eq 510) {
Write-Host "You got 2nd prize"
} elseif ($n -eq 999) {
Write-Host "You got 3rd prize"
} else {
Write-Host "Sorry, try for the next time"
}
Bash
#!/bin/bash
echo "Enter your lucky number"
read n
case $n in
101)
echo echo "You got 1st prize" ;;
510)
echo "You got 2nd prize" ;;
999)
echo "You got 3rd prize" ;;
*)
echo "Sorry, try for the next time" ;;
esac
PowerShell
#!/usr/bin/env pwsh
[int]$n = Read-Host "Enter your lucky number"
switch($n) {
101 { Write-Host "You got 1st prize" }
510 { Write-Host "You got 2nd prize" }
999 { Write-Host "You got 3rd prize" }
default { Write-Host "Sorry, try for the next time" }
}
Bash
#!/bin/bash
echo "Total arguments : $#"
echo "1st Argument = $1"
echo "2nd argument = $2"
PowerShell
Write-Host "Total arguments: $($args.Length)"
Write-Host "1st Argument: $($args[0])"
Write-Host "2nd Argument: $($args[1])"
Bash
#!/bin/bash
for arg in "$@"
do
index=$(echo $arg | cut -f1 -d=)
val=$(echo $arg | cut -f2 -d=)
case $index in
X) x=$val;;
Y) y=$val;;
*)
esac
done
((result=x+y))
echo "X+Y=$result"
PowerShell
#!/usr/bin/env pwsh
param([int]$X, [int]$Y)
$Result = $X + $Y
Write-Host "X+Y=$Result"
Bash
#!/bin/bash
string1="Linux"
string2="Hint"
echo "$string1$string2"
string3=$string1+$string2
string3+=" is a good tutorial blog site"
echo $string3
PowerShell
#!/usr/bin/env pwsh
$string1 = "Ironman"
$string2 = "Software"
Write-Host "$string1 $string2"
$string3 = $string1+$string2
$string3 +=" makes good software"
Write-Host $string3
Bash
#!/bin/bash
Str="Learn Linux from LinuxHint"
subStr=${Str:6:5}
echo $subStr
PowerShell
#!/usr/bin/env pwsh
$Str = 'Learn PowerShell from Ironman Software'
$subStr = $Str.Substring(6, 10)
Write-Host $subStr
Bash
#!/bin/bash
function F1()
{
echo 'I like bash programming'
}
F1
PowerShell
#!/usr/bin/env pwsh
function F1 {
Write-Host "I like PowerShell programming"
}
F1
Bash
#!/bin/bash
Rectangle_Area() {
area=$(($1 * $2))
echo "Area is : $area"
}
Rectangle_Area 10 20
PowerShell
#!/usr/bin/env pwsh
function RectangleArea {
$Area = $Args[0] * $Args[1]
Write-Host "Area is: $Area"
}
RectangleArea 10 20
Bash
#!/bin/bash
function greeting() {
str="Hello, $name"
echo $str
}
echo "Enter your name"
read name
val=$(greeting)
echo "Return value of the function is $val"
PowerShell
#!/usr/bin/env pwsh
function Greeting {
"Hello, $Name"
}
$Name = Read-Host "Enter your name"
$Val = Greeting
Write-Host "Return value of the function is $val"
Bash
#!/bin/bash
echo "Enter directory name"
read newdir
`mkdir $newdir`
PowerShell
#!/usr/bin/env pwsh
$newdir = Read-Host "Enter directory name"
New-Item $newdir -ItemType Directory
# mkdir also works
mkdir $newdir
Bash
#!/bin/bash
echo "Enter directory name"
read ndir
if [ -d "$ndir" ]
then
echo "Directory exist"
else
`mkdir $ndir`
echo "Directory created"
fi
PowerShell
#!/usr/bin/env pwsh
$newdir = Read-Host "Enter directory name"
if (Test-Path $newdir)
{
Write-Host "Directory exists"
} else {
New-Item $newdir -ItemType Directory
}
# shorter syntax
$newdir = Read-Host "Enter directory name"
if (gi $newdir -ea si)
{
echo "Directory exists"
} else {
mkdir $newdir
}
Bash
#!/bin/bash
file='book.txt'
while read line; do
echo $line
done < $file
PowerShell
#!/usr/bin/env pwsh
Get-Content 'book.txt'
Bash
#!/bin/bash
echo "Enter filename to remove"
read fn
rm -i $fn
PowerShell
#!/usr/bin/env pwsh
$fn = Read-Host "Enter a file to remove"
Remove-Item $fn
# Shorter syntax
$fn = Read-Host "Enter a file to remove"
rm $fn
Bash
#!/bin/bash
echo "Before appending the file"
cat book.txt
echo "Learning Laravel 5">> book.txt
echo "After appending the file"
cat book.txt
PowerShell
#!/usr/bin/env pwsh
Write-Host "Before appending the file"
Get-Content book.txt
"Learning Laravel 5" >> book.txt
Write-Host "After appending the file"
Get-Content book.txt
Bash
#!/bin/bash
echo "Wait command" &
process_id=$!
wait $process_id
echo "Exited with status $?"
PowerShell
#!/usr/bin/env pwsh
Start-Process notepad -Wait
Get-Process -Id $ProcessId | Wait-Process
Bash
#!/bin/bash
echo “Wait for 5 seconds”
sleep 5
echo “Completed”
PowerShell
#!/usr/bin/env pwsh
Write-Host "Wait for 5 seconds"
Start-Sleep 5
#shorter syntax
sleep 5
Write-Host "Completed"
Bash
curl -o newname.txt http://www.het.brown.edu/guide/UNIX-password-security.txt
PowerShell
Invoke-WebRequest http://www.het.brown.edu/guide/UNIX-password-security.txt -OutFile .\newname.txt
# shorter syntax
iwr http://www.het.brown.edu/guide/UNIX-password-security.txt -o newname.txt
Bash
# Search a file
grep error log.txt
# Search a directory
grep error *
# Recursively search a directory
grep -r error *
# List matching files
grep -l error *
# Measure number of objects
grep -c error *
# Display lines before and after
grep -C 2 error *
PowerShell
# Search a file
Select-String error log.txt
# Search a Directory
Select-String error *
# Recursively search a directory
Get-ChildItem * -Recurse | Select-String error
# List matching files
Select-String error * | Select-Object path
# Count Matches
Select-String error * | Measure-Object
# Display lines before and after
Select-String error *
Find this useful? Please consider sharing this article. Have a question about PowerShell? Contact us and we'll write a post about it.
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
Receive once-a-month updates about Ironman Software. You'll learn about our product updates and blogs related to PowerShell.