TExcelTAD
Home › Terminal & Command Line › Your First Script on Each OS

Your First Script on Each OS

A script is just a text file of commands that the shell runs one after another, exactly as if you had typed them. Anything you do twice in a terminal is a candidate for a script: setting up a project, backing up a folder, building and testing. In this lesson you write the same small "hello" script for each shell and learn the one or two rules that make it runnable on each OS.

Shell File extension First line How to run it
CMD (batch) .bat or .cmd @echo off hello.bat
PowerShell .ps1 (none needed) .\hello.ps1 (execution policy must allow it)
bash .sh (optional) #!/usr/bin/env bash chmod +x hello.sh, then ./hello.sh
zsh .zsh (optional) #!/usr/bin/env zsh chmod +x hello.zsh, then ./hello.zsh

Our script will do three things every script does: print a message, use an argument (a name, with a default if none is given) and use a variable computed inside the script.

Windows (CMD): a batch file

Create hello.bat in any editor (Notepad is fine). @echo off stops CMD from printing every command before running it. %1 is the first argument; %~1 is the same without surrounding quotes.

In the playground below, the batch file writes hello.bat for you and then calls it twice, once without and once with an argument:

cmd · try it yourself
@echo off
(
  echo @echo off
  echo set "name=%%~1"
  echo if "%%name%%"=="" set "name=World"
  echo echo Hello, %%name%%!
  echo echo This script is %%~nx0 in %%~dp0
) > hello.bat
type hello.bat
echo ---
call hello.bat
call hello.bat Ada
@echo off
set "name=%~1"
if "%name%"=="" set "name=World"
echo Hello, %name%!
echo This script is %~nx0 in %~dp0
---
Hello, World!
This script is hello.bat in C:\Users\learner\
Hello, Ada!
This script is hello.bat in C:\Users\learner\

%% in the outer script writes a single % into the file. On your own machine you'd just type the five lines of hello.bat into an editor and run it:

cmd
C:\Users\ada\scripts>hello.bat Ada
Hello, Ada!
This script is hello.bat in C:\Users\ada\scripts\

CMD runs .bat and .cmd files from the current folder without .\, and you can even leave out the extension: hello Ada works too. Double-clicking a .bat file in Explorer also runs it (the window closes at the end; add pause as the last line to keep it open).

Windows (PowerShell): a .ps1 script

PowerShell scripts use a param() block for arguments, which gives you named parameters and defaults for free:

powershell · try it yourself
@'
param([string]$Name = 'World')
"Hello, $Name!"
"This script is $(Split-Path -Leaf $PSCommandPath)"
'@ | Set-Content hello.ps1
./hello.ps1
./hello.ps1 -Name Ada
./hello.ps1 Grace
Hello, World!
This script is hello.ps1
Hello, Ada!
This script is hello.ps1
Hello, Grace!
This script is hello.ps1

Two Windows rules trip up every beginner:

  1. You must give a path. PowerShell does not run scripts from the current folder by name alone, for safety. Type .\hello.ps1, not hello.ps1.
  2. The execution policy may block scripts. On Windows client editions the default is Restricted in Windows PowerShell 5.1 (no scripts at all), while PowerShell 7 and Windows Server use RemoteSigned:
powershell
PS C:\Users\ada\scripts> .\hello.ps1
.\hello.ps1 : File C:\Users\ada\scripts\hello.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
PS C:\Users\ada\scripts> Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
PS C:\Users\ada\scripts> .\hello.ps1 -Name Ada
Hello, Ada!

RemoteSigned lets your own scripts run, and requires a digital signature only for scripts downloaded from the internet (Windows marks downloaded files). It's a safety belt against accidental double-clicks, not a security boundary. On macOS and Linux, PowerShell 7 has no execution policy, so ./hello.ps1 just runs, as in the playground above.

Linux & macOS (bash): a shell script

On Unix-like systems a file becomes runnable through two things: a shebang line (#!) that says which interpreter runs it, and the execute permission. The extension doesn't matter to the system; .sh is just a hint for humans.

bash · try it yourself
cat > hello.sh <<'EOF'
#!/usr/bin/env bash
name="${1:-World}"
echo "Hello, $name!"
echo "This script is $(basename "$0")"
EOF
./hello.sh 2>&1 | sed 's/^.*: //'
chmod +x hello.sh
./hello.sh
./hello.sh Ada
Permission denied
Hello, World!
This script is hello.sh
Hello, Ada!
This script is hello.sh

The first attempt fails with Permission denied because a new file isn't executable. After chmod +x it works. #!/usr/bin/env bash finds bash wherever it is on PATH, which is more portable than hard-coding /bin/bash. ${1:-World} means "argument 1, or World if it's empty". You can also skip the permission by naming the interpreter: bash hello.sh.

Why ./? For the same reason as PowerShell's .\: the current folder is not on PATH, so the shell must be told where the script is. Put your scripts in ~/bin or ~/.local/bin (and that folder on your PATH) to run them by name from anywhere.

macOS (zsh): the same idea

zsh is the default shell on macOS, and a zsh script is written the same way. zsh's $0 holds the script name, and ${1:-World} works identically. Here we also use print, zsh's own output command, and the :t modifier, zsh's short form of basename:

zsh · try it yourself
cat > hello.zsh <<'EOF'
#!/usr/bin/env zsh
name=${1:-World}
print "Hello, $name!"
print "This script is ${0:t}"
EOF
chmod +x hello.zsh
./hello.zsh
./hello.zsh Ada
Hello, World!
This script is hello.zsh
Hello, Ada!
This script is hello.zsh

On a Mac, scripts that only need standard features usually stay plain bash or sh scripts (macOS ships bash 3.2 plus zsh), so they run unchanged on Linux too. Write zsh scripts when you want zsh features. (A file named hello.command can also be double-clicked in Finder to run in Terminal.)

Common mistakes

Symptom Likely cause Fix
Permission denied (Linux/macOS) no execute bit chmod +x script.sh
command not found / not recognized script not on PATH run it as ./script.sh or .\script.ps1
running scripts is disabled execution policy Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
/usr/bin/env: 'bash\r': No such file file saved with Windows line endings save with LF (see Cross-Platform Scripts)
batch file prints every command missing @echo off add it as line 1

Next: real scripting

Each scripting language has a full course here: Batch Files, PowerShell Scripts & Scopes and Your First Bash Script. Script arguments are covered in depth in Batch Arguments and Bash Arguments.

Test yourself

Why does ./hello.sh fail with 'Permission denied' right after you create the file?

  1. The shebang is wrong
  2. The file has no execute permission
  3. Scripts must end in .bash
  4. You need sudo

Which line makes a script run with bash wherever bash is installed?

  1. @echo off
  2. #!/usr/bin/env bash
  3. param()
  4. // bash

How do you run hello.ps1 from the current folder in PowerShell?

  1. hello.ps1
  2. run hello.ps1
  3. .\hello.ps1
  4. call hello.ps1